c - some simple Pointers questions -


hi please kindly explain me why code generating error,

#include<stdio.h> int main(){     char ***x;     char **q = *x;     char **(*c) = x;     char ***d = &q;     char ***p = "asdasd";      x=p;      printf("d:%s\n",d);     printf("q:%s\n",q);     printf("x:%s\n",x);   return 0; 

}

output: 1 segmentation fault

hi replys if init x, still got segmentation fault on

printf("q:%s\n",q); 

the output , code shown below, please kindly advise why d:1231 instead of 1231123124 , why x=p change value of x instead of (x, q, d)

int main(){     char ***x = "1231123124";     char **q = *x;     char **(*c) = x;     char ***d = &q;     char ***p = "asdasd";      x=p;      printf("p:%s\n",p);     printf("d:%s\n",d); //  printf("q:%s\n",q);     printf("x:%s\n",x);     printf("c:%s\n",c);      return 0; } 

output: p:asdasd

d:1231

x:asdasd

c:1231123124

char **q = *x; 

here dereferencing uninitialized pointer.

it's undefined behaviour, in case results segfault (in practice code try dereference random memory location, or null if compiler initializes local variables (this typical debug/non-optimized builds)).


Comments