This is the program which was asked in an interview to predict the output
main()
{
char **p = 0;
printf("%d\n", ++p);
}
Can you please let me know why is the answer 4 here?
This is the program which was asked in an interview to predict the output
main()
{
char **p = 0;
printf("%d\n", ++p);
}
Can you please let me know why is the answer 4 here?
A char takes up 1 byte, but the pointer to that char takes up 4 bytes (an int32). So, p
is actually a pointer to an int. When you increment a pointer, it moves forward by the amount of bytes of the underlying datatype. So, p
moves forward by 4 bytes.
The size of a char
is one byte, but the size of a pointer (in 32bit code) is four bytes. The double pointer means you're pointing at four byte quantities, thus
p ++
Advances the pointer four bytes (to supposedly the next char *
), thus the output of the printf
is 4.