Completed: Yes Created: March 30, 2023 Tag: C MOC, C Pointers
the name of an array, is actually a pointer to the first element of the array.
Output:
0x7ffe70f9d8f0
0x7ffe70f9d8f0
- the address of
&array_name[0]
andarray_name
is the same. - It’s because the variable name points to the first element of the array.
Similarly:
&x[1]
is equivalent tox+1
andx[1]
is equivalent to*(x+1)
.&x[2]
is equivalent tox+2
andx[2]
is equivalent to*(x+2)
.- …
- Basically:
&x[i]
is equivalent tox+i
andx[i]
is equivalent to*(x+i)
.
Output:
0x7ffe70f9d8f0
0x7ffe70f9d8f0
Addres of the elements of an array
Output:
1450734448
1450734452
1450734456
1450734460
1450734448
Address of array x: 1450734448
-
There is a difference of 4 bytes between two consecutive elements of array x.
- It is because the size of
int
is 4 bytes (on our compiler).
- It is because the size of
-
Notice that, the address of &x[0] and x is the same. It’s because the variable name x points to the first element of the array.