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.
int array_name[] = {0,1,2,3,4,5};
// Get the memory address of the myNumbers array
printf("%p\n", array_name);
// Get the memory address of the first array element
printf("%p\n", &array_name[0]);
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)
.
int array_name[] = {0,1,2,3,4};
printf("%d\n", *(array_name)); // Value of the first element
printf("%d\n", *(array_name + 1)); // Value of the second element
printf("%d\n", *(array_name + 2)); // Value of the third element
printf("%d\n", *(array_name + 3)); // Value of the fouth element
printf("%d\n", *(array_name + 4)); // Value of the fifth element
Output:
0x7ffe70f9d8f0
0x7ffe70f9d8f0
Addres of the elements of an array
int array_name[] = {0,1,2,3,4};
printf("%p\n", i, &array_name[0]); // Addres of the first element
printf("%p\n", i, &array_name[1]); // Addres of the second element
printf("%p\n", i, &array_name[2]); // Addres of the third element
printf("%p\n", i, &array_name[3]); // Addres of the fouth element
printf("%p\n", i, &array_name[4]); // Addres of the fouth element
printf("Address of array x: %p", array_name);
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.