473,385 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

struct/union pointer/address stuff.



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.
Jun 27 '08 #1
1 1676
In article <7f**********************************@8g2000hse.go oglegroups.com>
Kenneth Bull <ke**********@gmail.comwrote:
[given]
>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
"Sort of". The problem here is that &a and &a.x have different
*types*. This is a bit like asking whether 3 and 3.0 are "the
same":

#include <stdio.h>
int main(void) {
if (3 == 3.0) puts("3 and 3.0 are identical");
if (sizeof 3 != sizeof 3.0) puts("3 and 3.0 are different");
return 0;
}

This program, run on most of my machines, says that 3 and 3.0 are
both identical *and* different (because they compare equal, but
"sizeof 3" is 4, and "sizeof 3.0" is 8).

If you convert &a and &a.x to some common (and thus comparable)
type, they will be equal, provided of course the conversion is
valid in the first place. For instance:

assert ((void *)&a == (void *)&a.x);

will never fail.

(Contrast this with, for instance:

if ((char)3 == (char)3.1415) puts("3 and 3.1415 are identical");

which claims that they are identical. After converting to one
common type, they are equal, but if we chose a different type --
such as "float" or "double" -- they would compare unequal. The
Standard says that, for the first element of the "struct", we can
choose *any* suitable type -- such as "struct foo *" or "void *"
-- and the results of the conversion will compare equal.)
>2) (&a.x < &a.y) && ( .. etc.. ) && ( &a.u < & a.z)
Again, these have type mismatches. If we convert them to suitable
comparable types, though, this does in fact hold in Standard C.
(It does not hold in some extended versions of C, including GNUC,
where a "zero sized array" may occupy no space.)
>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)
Yet again, we have the type mismatch problem, but if we convert all
the addresses to either "void *" or "union foo *", they will indeed
all compare equal.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Luc Le Blanc | last post by:
I have 2 APIs that store/recall a void *. Since all I need to store is a 32-bit struct, I pass the actual data (instead of a pointer to it) as a void *: typedef { UInt8 color;...
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
4
by: Everton | last post by:
The task at hand is to initialize a variable of type "struct in6_addr" in a portable way. For instance: /* NetBSD - /usr/include/netinet6/in6.h */ struct in6_addr { union { __uint8_t ...
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
18
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one...
2
by: Kenneth Bull | last post by:
Say, struct foo { int x; double y; /* etc. more variables defined */ short u; char z; } a;
10
by: Raman | last post by:
Hi All, Is it valid: struct test{ }; I mean, Can we have a struct containing no members? Is this a an
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.