Struct/Union pointer stuff

K

Kenneth Bull

Say,

struct foo
{
int x;
double y;
/* etc. more variables defined */
short u;
char z;
} a;


Are the following true on all systems?

1) &a == &a.x
2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)


Are the following true if we change 'struct' to 'union' above, on all
systems?

3) &a == &a.x
4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
equal for both ?s)


Thank you for the help.
 
P

Peter Nilsson

Kenneth said:
Say,

struct foo
{
int x;
double y;
/* etc. more variables defined */
short u;
char z;
} a;


Are the following true on all systems?

1) &a == &a.x
2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)

No, but you're almost there.

Note that the pointer types don't match. Your expressions
violate constraints. It is true to say that...

(int *) &a == &a.x
&a == (struct foo *) &a.x

offsetof(struct foo, x) == 0
offsetof(struct foo, x) < offsetof(struct foo, y)
offsetof(struct foo, y) < offsetof(struct foo, u)
offsetof(struct foo, u) < offsetof(struct foo, z)
Are the following true if we change 'struct' to 'union' above, on all
systems?

3) &a == &a.x

(int *) &a == &a.x
&a == (union foo *) &a.x
4) &a.? == &a.? where ?s can be x, y, ..., u, or z (not necessarily
equal for both ?s)

If the addresses are converted to void * or suitable type, they must
compare equal.

Note that it is not possible to take the address of bitfields.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top