473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Accessing memory address of 2d array using pointers

1
I have this piece of code in a funtion which is trying to retrieve the memory address of values that are stored in a 2D array, using pointers..[b].Could I get help or tips on how to do this please :)...I'm new at this programming..

void displayADD(float temp[][DAY])
{
int m,d;
float *ptr;
ptr=&temp[0][0];

*ptr=temp[m][d];}

}}
cout<<"The address storing this value is "<<&ptr<<endl;

}

Vinaka.
Aug 4 '06 #1
4 13761
Banfa
9,065 Expert Mod 8TB
That is not a complete code chunk (1 { 4 } make it hard to follow)

however

ptr=&temp[0][0];

Sets ptr to point to the very first entry in the arry

*ptr=temp[m][d];

Copys the value at m, d to where ever ptr is currently pointing to, in this example temp[0][0]

cout<<"The address storing this value is "<<&ptr<<endl;

Prints the address of ptr this is not the memory location of temp[0][0] but the memory location of ptr itself.

If you want to print the memory location of temp[0][0] you need

cout<<"The address storing this value is "<<ptr<<endl;
Aug 4 '06 #2
ptr=&temp[0][0],
after execution of this instruction ptr will be loaded with the address of 1st row 1st column of array temp,
matrix elements are stored row wise in C,
so suppose temp is a 5 by 6 matrix and you want to get the address of
temp[4][5],
simply increase individual subscripts by 1,multiply them and subtract 1 from the product,
so 4 is increased to 5,
5 is increased to 6,
their product is equal to 30,
subtract 1 from it,
which leads to 29,
so address of temp[4][5] could be obtained by
(ptr+29)
and its value by
*(ptr+29)
Aug 7 '06 #3
Banfa
9,065 Expert Mod 8TB
ptr=&temp[0][0],
after execution of this instruction ptr will be loaded with the address of 1st row 1st column of array temp,
matrix elements are stored row wise in C,
so suppose temp is a 5 by 6 matrix and you want to get the address of
temp[4][5],
simply increase individual subscripts by 1,multiply them and subtract 1 from the product,
so 4 is increased to 5,
5 is increased to 6,
their product is equal to 30,
subtract 1 from it,
which leads to 29,
so address of temp[4][5] could be obtained by
(ptr+29)
and its value by
*(ptr+29)
That unfortunately is wrong. You have the right sort of principle but have your maths wrong.

This is correct
Expand|Select|Wrap|Line Numbers
  1. int array[5][6];
  2. int *ptr = &array[0][0];
  3.  
  4. // Then array[4][5] can be accessed as
  5.  
  6. *(ptr+ (4*6)+5)
  7.  
in the code above
*(ptr+ (4*6)+5)

written descriptively is

*(ptr + (the number of the first index to access * the declared size of the second index) + the number of the second index)

4 * 6 + 5 = 29

This is the same as your result but that is only by chance of the numbers used

Look at the array entry

array[4][4]

By your method it is at (4+1)*(4+1)-1 = 24

but it is actually at (4*6)+4 = 28

Note that you can also see that array[4][4] should be 1 place behind array[4][5] and by my calculation it is (29 - 28 = 1).
Aug 7 '06 #4
sorry you are correct....
Aug 8 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: smartbeginner | last post by:
main() { int i; int *a; a=calloc(4,sizeof(*a)); /* The above code I know will not compile .But why cant I allocate space for all 4 integers i need to store */ for(i=0;i<2;i++)...
2
by: Bond | last post by:
i have written this program of entering 2-d array through pointers.this shows number of errors.please help me out. #include<stdio.h> #include<alloc.h> void aread(int*,int*,int*); void...
5
by: EJSpin | last post by:
Hey guys, I have put together a class called Mu class Mu { private: double **s; int velocity,numbers; double sPeak,muPeak,mu1; ...
1
by: niteshpanchal | last post by:
Hi, I want to assign array of 5 using pointer. How can i ?
1
by: niteshpanchal | last post by:
Hi, How to assign dynamic array using pointers for string.
0
by: ishraque510 | last post by:
Hello friends, I want to access the browsers address bar whenever user enters url into the address bar of browser and either presses enter or clicks on go. Then i want to change the url to, lets...
2
by: hanaa | last post by:
Hello I need to call a function that returns a 2D array. I need to dynamically allocate memory for the array in the callee. Now, I return using a pointer to a pointer. In the callee, I...
3
by: AnagJohari | last post by:
main( ) { int a = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf("%u %u %u %d \n",a,*a,**a,***a); printf("%u %u %u %d \n",a+1,*a+1,**a+1,***a+1); } If the location of an array start...
4
by: touches | last post by:
I am using an 2d array to search for the key 32 using pointers.. Now whenever i try to compile i get an linker error saying undefined symbol find_key(int *far,int,int).. I know the error is with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.