I am trying to understand how a pointer to an array works. A code snippet;
#include<stdio.h>
int main()
{
int arr[3] = { 0 , 8 ,10 };
int (*ptr)[3] = &arr;
int i = 0;
for (i = 0; i < 3 ; i++)
printf("Address (%p) - value( %d)\n", (*ptr+i) , *(*ptr + i));
return 0;
}
An asterisk * derefernces the ptr . If i = 1, why is (*ptr+i) = ith value not value at ptr + i.
The type of
ptrisint (*)[3](pointer to an array length 3 ofint). The type of*ptrisint[3](array length 3 ofint). In most expressions, an operand of typeint[3]is converted to aint *(pointer toint) pointing to the first element of the array. The expression(*ptr+i)results in a pointer to theith element of the array by pointer arithmetic. In the expression*(*ptr+i), the pointer to theith element of the array is dereferenced to produce the value of theith element of the array, which is of typeint.