473,785 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

&first arg of a union or struct

I have a union
union r32{
uint8_t l;
uint16_t x;
uint32_t val;
};

union r32 reg;

and a function
void f(union r32* a){a->x=1;}

then

to do f(&reg.x) or f(&reg.l) or f(&reg.val) is good or not?
is it &reg.x=&reg.l=& reg.val ?

Or I have to do f(&reg)?
Thank you
Nov 14 '05 #1
1 1237
RoSsIaCrIiLoIA wrote:
I have a union
union r32{
uint8_t l;
uint16_t x;
uint32_t val;
I'll assume the `uintxxx' are typedefs.
};

union r32 reg;

and a function
void f(union r32* a){a->x=1;}

then

to do f(&reg.x) or f(&reg.l) or f(&reg.val) is good or not?
Not valid as written. f() expects a `union r32*'
argument, and these three calls provide a `uint8_t*',
a `uint16_t*', and a `uint32_t*'. These pointer types
are not compatible with `union r32*'.

A union pointer can be converted to a pointer to
any of its members, and vice versa, but you must write
the conversion explicitly:

f( (union r32*) &reg.x )

.... which is pretty silly, but it works.
is it &reg.x=&reg.l=& reg.val ?
I'll assume you're asking about comparing the pointer
values, not somehow assigning them. Pointers can't be
compared unless they have compatible types, so the
compiler will object if you attempt `&reg.x == &reg.l'
(unless the `uintxxx' typedefs are aliases for the same
underlying C type, which seems rather unlikely).

If you convert the pointer values to a legitimate
common type -- `void*' or `union r32*' or `char*', for
example -- then all three of these will compare equal.
Or I have to do f(&reg)?
You don't *have* to, but it's certainly easiest.
Thank you


Judging from the context, you're hoping to do some
type-punning by placing a bunch of different objects in
a union, storing a value in one of them, and retrieving
another. Unfortunately, that invokes undefined behavior
(except in a special circumstance involving structs,
which doesn't seem to apply here). You will get away
with this dubious practice on many compilers -- but be
prepared for inexplicable breakage, especially if the
compiler uses aggressive optimizations.

--
Er*********@sun .com

Nov 14 '05 #2

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

Similar topics

2
2155
by: Claudio | last post by:
hi all i put in this email source code so u can copy and paste to verify strange first : in this example bit size is a BYTE ?! second : in the last printf output is wrong ? ? best regards all
0
1334
by: Codex Twin | last post by:
hello group: The following is a fragment from a schema which defines the EWethnicCategoryStructure type. As you can see, its type is defined by the SimpleType enumeration EWethnicCategoryType. EWethnicCategoryType is defined as a union with the CommonEthnicCategoryType SimpleType enumeration. Now when I use the XSD tool to create a C# class of this schema, the resulting code does not include the SelectedEthnicCategory enumeration.
4
21008
by: csudha | last post by:
Hi All, Can you give me the example code which explains difference between Structures and Union. As a newbie please do the needful. Thanks, csudha.
14
1575
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
18
1792
by: chump1708 | last post by:
union u { struct st { int i : 4; int j : 4; int k : 4; int l; // or int l:4 }st; int i;
1
1364
by: sarathy | last post by:
Hi, 1. I executed the following code. But could not deduce anything from the results. Can anyone please help me out. # include <stdio.h> struct struct1{ const :16; volatile :4;};
5
575
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits system + OS e.g. Windows, Linux, WinCE. I am not quite sure about the alignment of the memory.... soln. 1: should be faster, I am not sure. idx size (bytes) 1 4
6
4105
by: npd1 | last post by:
what is the difference between structure & union
34
1935
by: mdh | last post by:
Hi All, Just when I thought things were going to get easy! Structs. I **thought** I had copied the examples pretty closely, but am getting a number of errors. The code:
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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...
1
10091
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
9950
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...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
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
3
2879
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.