Interview Question
Software Engineer / DevelopersThe answer indeed is 5, with a DevC++ compiler for a C file.
But if you are right, p is pointing to address 1. If I change the next line as p--, it gives me -3 as the answer. How can it point to address -3?
guys its pretty clear that p is an integer pointer and incrementing that makes it point to the next integer and its perfectly legal to print the address of the pointer [printing the value pointed to may give segmentation fault errors]. Here is the output of compiling/running using gcc4.3 on linux:
kk@kk-laptop:~$ gcc temp.c
temp.c: In function ‘main’:
temp.c:5: warning: initialization makes pointer from integer without a cast
temp.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
temp.c:4: warning: return type of ‘main’ is not ‘int’
kk@kk-laptop:~$ ./a.out
5
it should be segfault since when we do int *p=1 it means we are assigning non lvalue to a pointer variable
- cunomad September 03, 2009