Following yesterday's question, I experimented some more with pointers. Specifically pointers of type int (*) [n]
Here's some code I wrote :
#include <stdio.h>
int main(void)
{
int a[5] = {1, 2, 3, 4, 5};
int (*p) [5] = &a;
int *q = a;
printf("\t\t\t\t\tp\t\tq\n\n");
printf("Original Pointers: \t%20d%20d", p, q);
printf("\n\n");
printf("Incremented Pointers:\t%20d%20d", p+1, q+1);
printf("\n\n");
printf("Pointer values: %20d%20d", *p, *q);
printf("\n\n");
return 0;
}
And here is it's output:
p q
Original Pointers: 132021776 132021776
Incremented Pointers: 132021796 132021780
Pointer values: 132021776 1
The pointer
p, jumps by 20 when incremented. Is this because it's a pointer of typeint(*)[5], and therefore jumps bysizeof(int) * number of columnsin the array ?Both
pandqhave the same values (but different types), and yet when using the indirection operator withp, I don't get the value at the first cell of the array, instead I get the value ofpitself printed out. Why is this?When I use
int *p = &a, it throws up a warning (because of different types of pointers, but later when I use the indirection operator with p, I can get it to print the value of the first cell in the array. Is this because when I assign&atop, it CONVERTS the type of&arr(which isint ( * ) [5]) to the typeint *, and then assigns top?