Why does void *p; p = p + 1; fail to compile as strictly conforming C?
Correct answer
C. Because pointer arithmetic needs a complete object type to know the element size
16 views
C. Because pointer arithmetic needs a complete object type to know the element size
16 views
Pointer arithmetic scales by the size of the pointed-to type, and void is an incomplete type with no size, so adding 1 is ill-formed in C. GCC accepts it as an extension by treating sizeof(void) as 1, which is why the code compiles widely. Portable code casts to unsigned char * first, since that type is guaranteed to have size 1.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.