12,063,242 members (54,209 online)
Rate this:
See more:
```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 5:29am

Rate this:

## 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

"ptr is..."```
We can't go right because we need a matching bracket first, so we go left
```int (*ptr)[4]=a;
(*ptr)

"ptr is a pointer to..."```
Now we have closed the brackets so we can go right again:
```int (*ptr)[4]=a;
(*ptr)[4]

"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 21-Apr-13 11:56am

Yes, it did.

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 337 CHill60 300 d@nish 175 Richard MacCutchan 145 Chris Maunder 135
 Dave Kreskowiak 1,827 Richard MacCutchan 1,225 d@nish 980 OriginalGriff 800 Sergey Alexandrovich Kryukov 733