1. The following code snippet uses minus operation on two pointers to
calculate the distance between struct members. This is illegal, right?
2. s1 and s2 are type of the same struct S. Can the distance of s1.i4
between i3 be used to deduce the distance between s2.i4 and s2.i3?
Thank you for your time.
#include <stdio.h>
#include <stddef.h>
struct S {
/*...*/
int i3;
/*...*/
int i4;
};
int main(void)
{
struct S s1, s2;
ptrdiff_t distance;
distance = &s1.i4 - &s1.i3;
s1.i3 = 11;
s1.i4 = 12;
s2.i3 = 13;
s2.i4 = 14;
printf("%d, %d\n", s2.i3, *(&s2.i3 + distance));
return 0;
}
calculate the distance between struct members. This is illegal, right?
2. s1 and s2 are type of the same struct S. Can the distance of s1.i4
between i3 be used to deduce the distance between s2.i4 and s2.i3?
Thank you for your time.
#include <stdio.h>
#include <stddef.h>
struct S {
/*...*/
int i3;
/*...*/
int i4;
};
int main(void)
{
struct S s1, s2;
ptrdiff_t distance;
distance = &s1.i4 - &s1.i3;
s1.i3 = 11;
s1.i4 = 12;
s2.i3 = 13;
s2.i4 = 14;
printf("%d, %d\n", s2.i3, *(&s2.i3 + distance));
return 0;
}
Comment