#include <stdio.h>
int main(void)
{
int a, b;
int *p = &a;
#ifdef __clang__
int *q = &b + 1;
#elif __GNUC__
int *q = &b - 1;
#endif
printf("%p %p %d\n", (void *)p, (void *)q, p == q);
}
C11 § 6.5.9 \ 6 says that
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.
I have tested it four different ways:
- Clang 9.0.1 with
-01
option; - Clang 9.0.1 without any options;
- GCC 9.2.0 with
-01
option; - GCC 9.2.9 without any options.
The results are the following:
$ ./prog_clang
0x7ffebf0a65d4 0x7ffebf0a65d4 1
$ ./prog_clang_01
0x7ffd9931b9bc 0x7ffd9931b9bc 1
$ ./prog_gcc
0x7ffea055a980 0x7ffea055a980 1
$ ./prog_gcc_01
0x7fffd5fa5490 0x7fffd5fa5490 0
What is the correct behavior in this case?