If int *p holds address 1000 and int is 4 bytes, what does p++ leave in p?
Correct answer
B. 1004, one int further along in memory
13 views
B. 1004, one int further along in memory
13 views
Incrementing a pointer advances it by sizeof(*p) bytes, not by one byte, so an int pointer moves 4 bytes and lands on 1004. This scaling is exactly what makes indexing work, since p + i must reach the i-th element rather than the i-th byte. The behaviour is defined only while the result stays inside the same array or one past its end.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is B? Ask here and get an explanation.