I'm screwing around with the macro offsetof. Just for the sake of screwing around with it. I can't figure out one piece. Here's my code:
I'm trying to figure out why rawPoint and rawPoint2 aren't the same addresses? I'm obviously missing something. Any ideas?Code:#include <stdlib.h> #include <stdio.h> #include <stddef.h> struct Object { int rc; }; struct RawPoint { int x,y; }; struct Point { struct Object _; int x; int y; }; int main(int argc, char ** argv) { struct Point * point = malloc(sizeof(struct Point)); point->x = 10; point->y = 10; size_t offset = offsetof(struct Point,x); struct RawPoint * rawPoint = (struct RawPoint *) (point + offset); struct RawPoint * rawPoint2 = (struct RawPoint *) &(point->x); //shouldn't rawPoint and rawPoint2 be the same address? printf("offset: %ld\n",(long)offset); printf("point: %p\n",point); printf("rawPoint: %p\n",rawPoint); printf("rawPoint2 %p\n",rawPoint2); return 0; }
Thanks!



LinkBack URL
About LinkBacks



