In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same.
since arrays in C
can decays to pointer with the same data type, (and the name of the array can decays to the address of the array or the address of the first element the same)
we can write:
int* ptr = array;
ptr is new data type which poits to the array start.
now what is common:
pointer1
and pointer 2
hold the same address points in memory to same address.
but there is some difference between them:
pointer1
is pointer to an array of 10 integer data types where pointer2
is pointer to one integer data type.
when derefrence
the pointers:
pointer2
deref to one int data type where pointer1
is deref to an array of 10 ints. so we can do with pointer1
:
(*pointer1)[5]=1000;
but this will be in valid in pointer2
.
the equavivalnt in pointer2
will be:
*(pointer2+5)=1000;
pointer arithmetics are valid on both, but the results of increment are different:
int* ptr_new = pointer1 + 1;
increments pointer1
by sizeof the whole array
(10 ints) and now ptr_new points to the last element.
int* ptr_new = pointer2 + 1;
increments pointer2
by sizeof(int)
and now ptr_new points to the second
element in the array.