Saturday, December 17, 2011

Segmentation Fault

What is the output and does it depend on underlying operating system we are using.

int main( )
{
int i=8;
int *p=&i;
printf("%d",*p);
*++p=2;
printf("%d",i);
printf("%d",*p);
printf("%d",*(&i+1));
return 0;
}
Useful Links:
http://en.wikipedia.org/wiki/Segmentation_fault

1 comment:

  1. When we are incrementing p, it is pointing to an invalid location,so it should generate a segmentation fault.

    This is irrespective of operating system.If you are using same compiler with right flag set then it will work in the same way.

    ReplyDelete