I was reading this question on stackoverflow C pointer to array/array of pointers disambiguation
I came across int (*q)[3]; // q is a pointer to array of size of 3 integers
The discussion was quite on understanding complex declarations in C.
Iam unable to understand when it is used and how it is used? how do I dereference it? could anyone explain me with some sample codes, like initializing the pointer and dereferencing it.
int main(){
int a =45;
int c[3] = {23};
int b[2][3];
int d[2][5];
int (*q)[3];
b[0][0]=1;
b[0][1]=2;
b[0][0]=3;
q = &a; // warning incompatible pointer type
q = c; // warning incompatible pointer type
q = b; // no warnings works fine
q = d; // warning incompatible pointer type
return 0;
}
After trying the above statements, I understood q can point to an array of n row but 3 column
sized array. How do I dereference those values?
printf("%d",*q); gives some strange value 229352.
Could anyone explain me how to initialize and how to dereference the pointers and its memory layout?