#include <stdio.h> int main(void) { int* ptr; int my_array[] = {11,22,33,44}; ptr = &my_array[0]; for (i=0;i<4;i++) { printf("my_array[%d] = %d ", i, my_array[i]); printf("ptr + %d = %d\n", i, (*ptr+1)); } return 0; }
my_array[0] = 11 ptr + 0 = 11; my_array[1] = 22 ptr + 4 = 22; my_array[2] = 33 ptr + 8 = 33; my_array[3] = 44 ptr + 12 = 44;
This shows the following facts about pointers and arrays:
The address of an array at element 0 (&my_array[0]) is the same as the array name.
The following statements are therefore equivalent: ptr = &my_array[0]; and ptr = my_array;.
An array is a pointer to the first element of the array.
But an array cannot be re-assigned like a pointer. While my_array == ptr the following code does not work:
int my_array[5]; int* prt; ptr = my_array; my_array = ptr; // ERROR: an array is NOT an lvalue