For the following code:
int (*ptr)[10];
int a[10]={99,1,2,3,4,5,6,7,8,9};
ptr=&a;
printf("%d",(*ptr)[1]);
What should it print? I'm expecting the garbage value here but the output is 1
.
(for which I'm concluding that initializing this way pointer array i.e ptr[10]
would start pointing to elements of a[10]
in order).
But what about this code fragment:
int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
*ptr=a;
printf("%d",*ptr[1]);
It is giving the segmentation fault.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…