By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

organization of struct elements in the memory.

P: n/a
hello,

Is the following code proper?

-----
typedef int one_data;
typedef long two_data;
typedef struct one_{
char *name;
one_data info;
}one;
typedef struct two_{
char *name;
two_data info1; // not the same as one_data.
}two;

typedef union{
one o;
two t;
}composite;

int main(){
composite c;
c.o.name="a";
printf("%s\n",c.t.name); // can i access the common prefix field?
}
---

In other words, I'm assuming that if instances of two structs start at
the same memory location, then all the common starting elements can be
accessed using either handle. Is this assumption valid across all
compilers/platforms?

Mar 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"aiooua" <ai****@gmail.comwrote:
typedef struct one_{
char *name;
one_data info;
}one;
typedef struct two_{
char *name;
two_data info1; // not the same as one_data.
}two;

typedef union{
one o;
two t;
}composite;

int main(){
composite c;
c.o.name="a";
printf("%s\n",c.t.name); // can i access the common prefix field?
Yes, but...
}

In other words, I'm assuming that if instances of two structs start at
the same memory location,
....only if they're members of the same union, which is the only way this
assumption can reasonably (i.e., without reprobate pointer hackery) hold
true.
It would be a very unusual implementation indeed which did not allow
this kind of aliasing even when the two structs are _not_ members of the
same union, but the Standard does not guarantee that.

Richard
Mar 8 '07 #2

P: n/a
aiooua wrote:
hello,

Is the following code proper?

-----
typedef int one_data;
typedef long two_data;
typedef struct one_{
char *name;
one_data info;
}one;
typedef struct two_{
char *name;
two_data info1; // not the same as one_data.
}two;

typedef union{
one o;
two t;
}composite;

int main(){
composite c;
c.o.name="a";
printf("%s\n",c.t.name); // can i access the common prefix field?
}
---

In other words, I'm assuming that if instances of two structs start at
the same memory location, then all the common starting elements can be
accessed using either handle. Is this assumption valid across all
compilers/platforms?
If they're within the same union, then yes, otherwise, the standard
doesn't guarantee it. However, for any one platform, it'll usually
work, with a pointer cast.

Mar 8 '07 #3

P: n/a
On Mar 8, 8:45 pm, "santosh" <santosh....@gmail.comwrote:
aiooua wrote:
hello,
Is the following code proper?
-----
typedef int one_data;
typedef long two_data;
typedef struct one_{
char *name;
one_data info;
}one;
typedef struct two_{
char *name;
two_data info1; // not the same as one_data.
}two;
typedef union{
one o;
two t;
}composite;
int main(){
composite c;
c.o.name="a";
printf("%s\n",c.t.name); // can i access the common prefix field?
}
---
In other words, I'm assuming that if instances of two structs start at
the same memory location, then all the common starting elements can be
accessed using either handle. Is this assumption valid across all
compilers/platforms?

If they're within the same union, then yes, otherwise, the standard
doesn't guarantee it. However, for any one platform, it'll usually
work, with a pointer cast.
thanks for the replies. they were useful.

Mar 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.