Click here to Skip to main content
11,578,006 members (63,387 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: c/c++
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int (*ptr)[4]=a;
what do ptr and *ptr mean in this context? Why ptr and *ptr point to the same meory address
Posted 21-Apr-13 4:29am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

ptr is a variable, in this case it is declared as a pointer to an array of 4 integers.

To read the declaration, start with the name of the variable (ptr in this case) and work out, going right when possible:
int (*ptr)[4]=a;
"ptr is..."
We can't go right because we need a matching bracket first, so we go left
int (*ptr)[4]=a;
"ptr is a pointer to..."
Now we have closed the brackets so we can go right again:
int (*ptr)[4]=a;
"ptr is a pointer to an array of four...
The equals ends teh declaration, so we have to go left again
int (*ptr)[4]=a;
int (*ptr)[4]=a;
"ptr is a pointer to an array of integers"
And the rest of teh line assigns a suitable value.

So when you use ptr later in your code, you are referring to the instance of an array of three arrays of four integers.
When you refer to *ptr in your code you are referring to one of the arrays of four integers, because the "*" dereferences the pointer.

Did that make sense?
CPallini at 21-Apr-13 11:56am
Yes, it did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 165
1 Richard MacCutchan 165
2 Peter Leow 160
3 Afzaal Ahmad Zeeshan 125
4 Frankie-C 85
0 OriginalGriff 880
1 Sergey Alexandrovich Kryukov 685
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 393

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100