UPDATE: A possible duplicate of this question has been reported. While the answers submitted to that question partially solves this one, to explain why we can't use double pointers to represent two dimensional arrays, it does not specify how to access the correct memory address of values which are stored in the pointer; which is the primary theme of this question. The marked answer in this question does an excellent job of clarifying, to anyone confused in the future, about why this is a misunderstood concept and alternative ways on how it can be resolved. This may be useful for future readers who would like to trace the output to understand the concept better!(If you look closely at the comments by the OP in that question, you will realise that the tracing is not clear enough for him to understand. A part of that question present here gives a much better tracing example so that just about anyone can easily understand)
(rest of the question is in its un-edited glory for future readers to reflect upon similar mistake if they happen to make it.)
What I'm trying to do:
- Trying to see how pointers are used instead of 2D arrays
- In this case, I'm inserting values to a 2D array(namely, a[ ][ ])
- I am trying to access the values in that array with a pointer(namely, ptr)
- I am also trying to trace the memory address from which those values are being taken from
- Trying to know where I'm going wrong? (would appreciate an appropriate explanation and if possible, a link to reference and learn further)
What I've tried:
Below is a sample code:
int main()
{
int a[2][2]={{5,7},
{0,1}};
int** ptr=(int**)a;
int i,j;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d printing from==>%x\n",&ptr[i][j],((&ptr[i])+j));
}
}
getch();
return(0);
}
In the above code, I'm trying to print the values present inside matrix a[ ][ ] with the help of pointer ptr. In doing so, I am tracing the address from where(in the memory address) the printing actually takes place. I seem to have encountered a disastrous output in the form of:
5 printing from ==>12ff7c
9 printing from ==>12ff80
7 printing from ==>12ff80
11 printing from ==>12ff84
Then I modified the line:
printf("%d printing from==>%x\n",&ptr[i][j],((&ptr[i])+j));
with this:
printf("%d printing from==>%d\n",&ptr[i][j],*((&ptr[i])+j));
to know which values were actually present in those addresses:
5 printing from ==>5
9 printing from ==>7
7 printing from ==>7
11 printing from ==>0
My take on this is that: &ptr[i][j] is equivalent to (*(&ptr[i])+j)) and ((&ptr[i])+j)) doesn't really tell me the addresses of even those numbers because &ptr[i] defines an address which is incremented by j in this case(but since j is reset to 0 in each outer loop iteration; that will cause same memory address to be pointed twice) Am I wrong with this chain of thought?
How can you help me?
- Here is my code failing miserably, and based upon the article on Double Pointer and Two Dimensional Arrays in C that I read here. Have I fundamentally misunderstood it?
- Could you post a correct cpp code by modifying my given code here and posting your own perma-link in the comments?(You can do that by clicking the "GET URL" button below the coding area in that site)
- I would highly appreciate an answer based on C instead of C++ where typedef ans such is possible I am trying to learn pointers better