Using pointer syntax in an array
I have some problem understanding the use of pointer syntax in context with 2-D arrays, although I am comfortable with 1-D character and array pointers, below is one of the syntaxes and I cannot figure out what the following expression looks like: evaluated.
To access the element stored in the third row of the second column of array a , we will use the indexed notation as a [2] [1] Another way to access one element is
*(a[2]+1)
and if we want to use it as pointers we will do it like
*(*(a+2)+1)
Although I can figure out the replacement *(a[2]+1)
how *(*(a+2)+1)
, but I don't know how it is evaluated. Please explain please an example. Suppose the array is in line order and contains the following elements
int a[5][2]={
21,22,
31,32
41,42,
51,52,
61,62
};
and the base address of the array is 100 (assume) , so the address is a [2] 108 (size int = 2 (another guess)) So, the expression*(*(a+2)+1). How does it gets evaluated does it start from the inside bracket and if it does then after the first bracket we have the value to which 1 is being added rather than the address... :/
To start with
a[i] = *(a+i);
So,
a[i][j] = *(a[i] +j)
and
a[i][j] = *(*(a+i) + j)
Like a [i] = * (a + i):
If a
is an array then the starting address of the array is given &a[0]
or justa
Therefore, when you specify
a[i]
this will decay the pointer operation *(a+i)
, which starts at the location a
and plays out the pointer to get the value stored in the location.
If a memory location a
, then the value stored in it is determined *a
.
Similarly, the address of the next element in the array is given
&a[1] = (a+1); /* Array decays to a pointer */
Now the location where the item is stored, given by &a[1]
or (a+1)
, so the value stored in that location is determined by *(&a[1])
or*(a+1)
For example:
int a[3];
They are stored in memory as shown below:
a a+1 a+2
------------------
| 100 | 102 | 104|
------------------
&a[0] &a[1] &a[2]
Now a points to the first element of the array. If you know the operations of the pointer a+1
, you get the next location, etc.
The 2D arrays below show what access is:
int arr[m][n];
arr:
will be pointer to first sub array, not the first element of first sub
array, according to relationship of array & pointer, it also represent
the array itself,
arr+1:
will be pointer to second sub array, not the second element of first sub
array,
*(arr+1):
will be pointer to first element of second sub array,
according to relationship of array & pointer, it also represent second
sub array, same as arr[1],
*(arr+1)+2:
will be pointer to third element of second sub array,
*(*(arr+1)+2):
will get value of third element of second sub array,
same as arr[1][2],
source to share
A 2D array is actually a sequential piece of memory. Let me give you an example: int a[3][4]
represents in memory a unique sequence of 12 integers:
a00 a01 a02 a03 a04 a10 a11 a12 a13 a14 a20 a21 a22 a23 a24
| | |
first row second row third row
(of course it can be expanded to any muti-dimensional array)
a
is an array of int[4]
: it decays to a pointer to int[4]
(in fact it decays to &(a[0])
)
a[1]
- the second line. It splits to int *
, indicating the start of the first line.
Even if arrays are not pointers, the fact that they decay into pointers allows them to be used in pointer arithmetic: a + 1
is a pointer to the second element of array a.
Anything that explains why a[1] == *(a + 1)
The same reasoning can be applied to a[i] == *(a+i)
and from there to all the expressions in your question.
Let's look specifically at *(*(a+2)+1)
. As stated above, a + 2
is a pointer to the third element of the array int 4
. So, *(a + 2)
is the third line, is an array int[4]
, and breaks down into int *
: &(a[2][0])
.
Since *(a + 2)
breaks up int *
, we can still use it as a basis for the pointer arithmetic, and *(a + 2) + 1
- a pointer to the second element of the third row: *(a + 2) + 1 == &(a[2][1])
. Just play it all out and we'll get it
*(*(a + 2) + 1) == a[2][1]
source to share