On Tue, Feb 25, 2014 at 5:05 AM, <j.e.ha...@gmail.com> wrote: > typedef struct { > int value; > } Number; > > Number *o; > o = malloc(sizeof(*o)); > o->value=3; > printf("o<%p>, o->value<%p>\n", o, &o->value); > > o<0x9fe5008>, o->value<0x9fe5008> > > Is the compiler borked?
No, because a structure in C is simply a laying-out of its members. A pointer to the structure is exactly the same as a pointer to its first member, same as a pointer to an array is really just a pointer to its first element (with other elements laid out sequentially, possibly with padding for alignment). In Python, a complex object is a thing in its own right; it has an identity and a set of members, each of which has its own identity. In C, the structure is an invisible piece of nothingness that surrounds an aggregation of other objects. For instance: typedef struct { int val1; int val2; } NumberPair; NumberPair five_pairs[5]; int *ten_ints = (int *)five_pairs; This is considered naughty (pointer aliasing), and can make a mess of optimization, but as far as I know, it's never going to be a problem (I don't think it's possible for there to be any padding between two ints; it's possible the struct will demand coarser alignment than the ints would, which is why I've written them in that order). You can access ten integers from the aliased pointer, and five pairs of integers through the original structure array, and they're all exactly the same. If you print out those two pointers, they will have the same value in them, and &five_pairs[3]->val2 will be the same as &ten_ints[7] (and the same as ten_ints+7). For in C, neither the structure nor the non-structure is anything. The only thing that counts is code expressing itself through love. (Galatians 5:6, K&R translation. Or something like that.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list