#include<stdio.h>
void main() {
int s[4][2]={
{1,2},
{3,4},
{5,6},
{7,8}
};
int (*p)[2]; // what does this statement mean? (A)
int i,j,*pint;
for(i=0;i<=3;i++) {
p=&s[i];
pint=(int*)p; // what does this statement mean? (B)
printf("\n");
for(j=0;j<=1;j++) {
printf("%d",*(pint+j));
}
}
I cannot understand Statement ‘A’ and ‘B’ .How and what has been done?
please explain this very clearly.
Statement A is a declaration
int (*p)[2]; ^ p is int (*p)[2]; ^ p is a pointer int (*p)[2]; ^ p is a pointer to an array int (*p)[2]; ^ p is a pointer to an array of 2 int (*p)[2]; ^^^ p is a pointer to an array of 2 intStatement B is an assignment expression with an embedded cast
pint=(int*)p; ^ take the value in p (of type "pointer to array of 2 ints") pint=(int*)p; ^^^^^^ take the value in p, convert it to 'pointer to int' even if it doesn't make sense to do so pint=(int*)p; ^^^^^ take the value in p, convert it to 'pointer to int' and put the resulting value (whatever that may be) in pintCasts are bad. Avoid casts as much as possible.
(*) except in very particular circumstances like
<ctype.h>or variadic functions or …