2 ms·
Then let's make them real arrays: #include <stdio.h> #define N 8 int main(void) { int a[N], b[N]; int *p = &a[0]; int *q = &b[N]; print
by tzs 8y ago
Then let's make them real arrays:
#include <stdio.h>
#define N 8
int main(void) {
int a[N], b[N];
int *p = &a[0];
int *q = &b[N];
printf("%p %p %d\n", (void *)p, (void *)q, p == q);
return 0;
}
Results:
$ gcc -std=c11 -O1 ar.c; ./a.out
0x7ffd32d048c0 0x7ffd32d048c0 0
$ gcc -std=c11 ar.c; ./a.out
0x7ffe3f8ccd60 0x7ffe3f8ccd60 1
- deleted 8y ago[deleted]
- smolsky 8y agoBTW, things work correctly when the objects live inside a real array: #include <stdio.h> int main(void) { int arr[16]; int *p = &arr[0] + 1; int *q = &arr[1]; printf("pointers: %p %p %d\n", p, q, p == q); return 0; } The result is perfectly correct and predictable, as the Standard guarantees pointer comparisons in this case: pointers: 0x71b35ac790d4 0x71b35ac790d4 1 The original code in the article relies on UB, I think, so all bets are off.