int a[5]; int *p = a + 5; is this pointer valid?
Correct answer
D. Yes; forming a pointer one past the last element is allowed, but dereferencing it is not
13 views
D. Yes; forming a pointer one past the last element is allowed, but dereferencing it is not
13 views
One-past-the-end pointers are expressly permitted so that half-open ranges from a to a + 5 work, but the location holds no object, so dereferencing or stepping further is undefined. Comparing against a + 5 is well defined, which is exactly what for (p = a; p != a + 5; ++p) relies on. The element a[5] itself is out of bounds and must never be accessed.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.