473,385 Members | 1,610 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.

&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 1219
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
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...
0
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....
4
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
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
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
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; ...
5
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...
6
by: npd1 | last post by:
what is the difference between structure & union
34
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
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.