Output predict

int main()
char *p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf+1)[5];
printf("%d \n", p);

Guess the o/p and what if we change p to *p in last printf statement.


  1. p stores address.
    here p is assigned *(buf+1+5)=9 as its value. The format specifier for pointers is %p but as we are using %d, it will print the signed value, i.e 9.

    As we are arbitrarily assigning any address value to p, so if we try to dereference it, it will generate a segmentation fault on unix compilers.

  2. yeah jainendra very good explanation...


Post a Comment

Popular posts from this blog