For the given code below :
If we place name
inside main
, I get a segmentation fault. Why?
Can we print each element of the matrix using p
?
Why does p++
jump by 8 bytes, while cp++
jumps to the next string?
char *name[] = {"Arza", "Homes"}; //NULL is added as the third-string? If this line goes into main, segfault?
int main(int argc, char *argv[]){
int matrix[][4] = {{1,2,3,4}, {5,6,7,8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
int i,j,*d,**p; //can we print each element of matrix using p?
d = p = matrix;
for(i = 0; i < 4; i++){
printf("darray[%d] = { ",i);
for(j=0;j<4;j++) {
printf(" 0x%X %d ",*d++, *p++); //Why does p jump 2 ints?
}
printf("}\n");
}
char ** cp;
cp = name;
printf("Name is : ");
while(*cp)
printf("%s ",*cp++); //how does cp jump to next string no matter the size of each string?
}