473,663 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struct/Union pointer 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
2 3571
Kenneth Bull wrote:
>
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)
Yes to all.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #2
Kenneth Bull wrote:
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.

--
Peter
Jun 27 '08 #3

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

Similar topics

1
4023
by: Robert Rota | last post by:
Hello, Please help me with the following struct development: I need three levels of a data structure simulating a 3 level pagetable. I need a structure which has a pointer to an array of pointers. The array size will be inputted as a command line arg. These pointers will point to another structure of the same type. Therefore we will have a pointer to an array of ptrs each pointing to an array which is of a different size then the...
2
5050
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 compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
67
10707
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 other. All pointers to union types shall have the same representation and alignment requirements as each other. Does it mean that *all* structure (or union) types have the same alignment? Eg. type
7
1702
by: elmafiacs | last post by:
I need some help with an error I'm getting... Sorry if this is basic stuff, but I'm still a newbie on C programming (working up, though). Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff). Sorry about the long subject, I didn't know how to explain it better... struct small { int num; };
6
2685
by: S.Tobias | last post by:
I'm trying to understand how structure type completion works. # A structure or union type of unknown # content (as described in 6.7.2.3) is an incomplete type. It # is completed, for all declarations of that type, by ^^^ # declaring the same structure or union tag with its defining # content later in the same scope. ^^^^^ (6.2.5#23)
32
8845
by: Weiguang Shi | last post by:
Hi, Is there a tool that, given a struct definition, generates a function that parses binary data of this struct and a command that can be used to construct binary data according to user-specified values for the fields of this struct? Thank you. Wei
4
2583
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 } type; union { int (*func)(int); struct menu_item *submenu; } action;
18
6078
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 variable. I manipulate to change 8-bit data before it causes to change 16-bit data and 32-bit data. For example. union {
1
1688
by: Kenneth Bull | last post by:
Say, struct foo { int x; double y; /* etc. more variables defined */ short u; char z; } a;
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.