I tried the below-pasted code and got an error:
cannot convert
int (*)[6]toint*in assignment
compilation terminated due to-Wfatal-errors.
#include <stdio.h>
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
int main(void)
{
int i;
ptr = &my_array; /* point our pointer to the first
element of the array */
printf("\n");
for (i = 0; i < 6; i++)
{
printf("my_array[%d] = %d ",i,my_array[i]); /*<-- A */
printf("ptr + %d = %d\n",i, *(ptr + i)); /*<-- B */
}
return 0;
}
The type of
&my_arrayisint (*)[6]while the type ofptrisint*. They’re incompatible types.What you should be doing is this:
Now the type
my_arrayisint[6]which decays intoint*in the above context. So it works.