Interview Question
Software Engineer / DevelopersCountry: United States
The behavior is undefined.The explanation goes as follows:
If you want to change the pointer p to point to another memory location, you must use &p in scanf.
However, if you write p, then you must supply the format specifier as %d, because by p, you are actually passing the memory location where the pointer p is pointing[i.e. i]. So, whatever you input that gets stored in i indirectly.
Note that in printf, if the format specifier is changed then the programmer is responsible for all kinds of side effects.
Hey *p does not print 10 because of the scanf statement. You are taking a user input for p and then printing the value in *p that is why
There's an unusual behavior with the printing of *p .Following will be the output of *p :
- Shobhit July 01, 2012Enter the pointer -------> Output in printf (*p)
1 -----------1
3 ----------- 3
5 ----------- 5
9 ----------- 9
10 ----------- 16
11 ----------- 17
14 ----------- 20
20 ----------- 32