I'm curious about a sentence in the C18 standard:
Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space. § 6.5.9 6
Why does the object following the array have to be necessarily another array? Couldn't simply be an object of the same type as the array base type (like an int immediately following an int[])?
No wonder I've tried this code:
#include <stdio.h>
struct test { int arr[10]; int i; };
int main() {
struct test t;
int *p, *q;
p = t.arr + 10;
q = &t.i;
if (p == q)
printf("Equal pointers.");
return 0;
}
And it yields equal pointers. Is this behaviour not guaranteed at all, just an implementation-defined coincidence?