In C syntax this line declares an array of 5 pointers to int
.
int* arr[5];
Below an array of 5 int
s is declared.
int arr[5];
The line above told the compiler to reserve in memory 5 times the size of int
in adjacent addresses of memory starting at &arr[0]
. While arr
is not a pointer, itself, if used as a name, arr
, decays to a pointer to the first slot (so arr
is equivalent to &a[0]
).
int* p = arr;
p
is an ordinary pointer to int
, assigned to a valid memory address, the same as arr
(or &arr[0]
)
Arrays and pointers differ. The first difference is 'arr' is a constant and can't be reassigned to other addresses.
For example the following is invalid
arr++;
While the following is valid
p++;
Furthermore pointers require extra dereference underlying ASM operations, whereas arrays don't
char arr[7];
char *p = arr;
Arrays indexing
Compare this line and its ASM instructions:
char a = arr[7];
0041137E mov al,byte ptr [_array_place+7 (417007h)]
00411383 mov byte ptr [a],al
Pointer indexing:
With this line and its ASM instructions:
char b = p[7];
00412DD7 mov eax,dword ptr [ptr_arg]
00412DDA mov cl,byte ptr [eax+7]
00412DDD mov byte ptr [b],cl
Arrays can decay to pointers to the first element. This happens frequently, e.g. when you pass them as function arguments. In that case, treat them as pointers inside the called function. You can eventually label them as constant pointers or pointers to constant or constant pointers to constant.
I hope this clarifies.