473,902 Members | 6,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing a union's field to a function

Hello!

The sample program below is compiled fine by gcc (with -Wall), but rejected
by Sun's SUNWspro compiler (version 6 update 2).

The point of contention is, whether a value for one of the union's types can
be passed to a function directly -- without creating a separate variable of
the union type and assigning the appropriate field of it.

Is gcc being too liberal, or is this behavior simply part of a newer
C-standard, which Sun's old compiler is not supporting? Thanks!

-mi
Aug 11 '06
50 6520
Mikhail Teterin <us****@aldan.a lgebra.comwrite s:
Ian Collins wrote:
>>>>Your program appeared in my newsreader as an attachment. Please don't
post attachments here

Why? Is not a (text) attachement easier to deal with? None of the "----
cut here ---" nuissance, just save into a file and compile...
Some people use news readers that don't support attachments. On Usenet,
go for the lowest common denominator, plain text.

Come on -- that's not a valid argument. The attachements have appeared more
than 10 years ago and all news-readers (including text based ones) now
support them.
Look around this newsgroup. People post code all the time; I think
yours may have been the first non-spam attachment I've ever seen here.

Apparently not all newsreaders handle attachments sanely. They do all
handle plain text sanely.

When in Rome ...

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 11 '06 #21
Mikhail Teterin schrieb:
Keith Thompson wrote:
>>You can't cast to a union type in either C90 or C99. gcc apparently
provides this as an extension. This is mentioned in the "C
Extensions" section of the gcc documentation. That documentation will
also tell you about the "-ansi -pedantic" or, if you prefer, "-std=c99
-pedantic" options, which would have warned you about this.

Thanks for the most useful answer... Any ideas on why this is not part of
the C-standard (yet?)? What's wrong with this extension -- what does it
break? Yours,
There are no clear, standardized rules.
Example:
What happens for
union foo {
float bar;
long double baz;
void *qux;
int *quux;
};
if you perform
union foo Foo = 0;
As a cast is only an explicit conversion, this is the same
as
union foo Foo = (union foo) 0;

Do we get the representation of 0.0F, 0.0L, (void *)0, or (int *)0?
Note: the latter two may in theory differ.

In C99, you have compound literals to work around that (note:
compound literals look like casts but are not casts -- they
yield lvalues):

,---
#include <stdio.h>

enum which {
eWhichInvalid = -1,
eWhichFlt,
eWhichLdbl,
eWhichVPtr,
eWhichIPtr
};

union foo {
float bar;
long double baz;
void *qux;
int *quux;
};

void blib (enum which Which, union foo Foo)
{
switch (Which)
{
case eWhichFlt:
printf("%g\n", Foo.bar);
break;
case eWhichIPtr:
printf("%p\n", (void *) Foo.quux);
break;
default:
puts("Too lazy for more");
break;
}
}

int main (void)
{
blib(eWhichFlt, (union foo){.bar = 0});
blib(eWhichIPtr , (union foo){.quux = 0});
return 0;
}
`---

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 11 '06 #22
Keith Thompson wrote:
If you're on a Unix-like system, you can filter your text through
"expand" before posting it.
Instead I chose to post the program as an attachment, which is supposed to
allow verbatim file transfer (together with the file's type and name).

And got flamed by users of broken news-readers and their sympathizers...

Lowest common denominator indeed :-(

-mi
Aug 11 '06 #23
Keith Thompson wrote:
Apparently not all newsreaders handle attachments sanely. šThey do all
handle plain text sanely.
Actually, they don't -- many use proportional fonts, which is rather a
nuissance, when it comes to software code.
When in Rome ...
:-) Atilla never knew that one, I'm sure...

-mi
Aug 11 '06 #24
Mikhail Teterin wrote:
Keith Thompson wrote:

>>If you're on a Unix-like system, you can filter your text through
"expand" before posting it.


Instead I chose to post the program as an attachment, which is supposed to
allow verbatim file transfer (together with the file's type and name).

And got flamed by users of broken news-readers and their sympathizers...

Lowest common denominator indeed :-(
If you post here asking for advice, you follow the conventions (which
are pretty well universal across Usenet). Whether those conventions
make sense or not is moot.

--
Ian Collins.
Aug 11 '06 #25
Mikhail Teterin wrote:
Ian Collins wrote:

In my case the type is specified by an earlier argument (stripped away for
the sample program posted). Same thing, really.
Don't do that, it causes threads to wander off where thy wouldn't go if
you posted the correct code.
>
> X x;
x.key = Int;
x.value.i = 42;

testfunc( &x );


Right, that would work. But is not:

testfunc(Int, 42);

easier and neater? It also leaves no way for the callee to modify the
caller's variable, thus allowing better compiler optimizations.. .
Maybe neater in this contrived case (leaving aside the subjective
opinion on compiler optimisations), but not where the object has to be
passed though a call chain, better to encapsulate all of the pertinent
information in a single object.

Your version may appear neater, but it isn't C. if you want a cleaner
syntax, use a different language. C is explicit, you either accept it,
or use something else.

If you want the passed object to be const, declare it const.

static void testfunc( const X* x ) {...}
By now I've learned from this thread, that I'm seeing a gcc-extension in
action. Why is such an extension not adopted by the C-standard remains my
question, though...
Because it's horrible?

--
Ian Collins.
Aug 11 '06 #26
On Fri, 11 Aug 2006 17:56:39 -0400, Mikhail Teterin
<us****@aldan.a lgebra.comwrote :
>Keith Thompson wrote:
>Ugh, that's worse. In my newsreader, it shows up with a bunch of
non-printable '\232' characters.

I just saved it back to a file from the news-reader. The file is perfectly
clean, opens up fine in vi, and compiles...
Here's what I got:

typedef union {
ššššššššintšššš ši;
ššššššššvoidššš š*p;
ššššššššstruct {
ššššššššššššššš šintššššši;
ššššššššššššššš šintšššššj;
šššššššš}šššššš šs;
} testunion;
>
>I don't know how they got there, but try to use only spaces for
indentation. šCopy and paste plain text if you can.

BSD's style(9) mandates use of tabs for indentation :-)
So? I didn't know BSD's style guide applied to newsgroup postings. Not
that it's much better in source code.
http://www.freebsd.org/cgi/man.cgi?q...tyle&sektion=9

Yours,

-mi
--
Al Balmer
Sun City, AZ
Aug 11 '06 #27
Al Balmer wrote:
Here's what I got:

typedef union {
ššššššššintšššš ši;
ššššššššvoidššš š*p;
ššššššššstruct {
ššššššššššššššš šintššššši;
ššššššššššššššš šintšššššj;
šššššššš}šššššš šs;
} testunion;
Just curious, which news reader?

--
Ian Collins.
Aug 11 '06 #28
Ian Collins wrote:
If you post here asking for advice, you follow the conventions (which
are pretty well universal across Usenet).š
You should note, that the only two people, who provided actual advice on the
subject matter -- Keith T. and yourself -- both had no problems reading the
attachment :-)
Whether those conventions make sense or not is moot.
Which is why I started a separate thread under the subject "Posting sample
C-code as attachment" -- to discuss changing the convention. Yours,

-mi
Aug 11 '06 #29
Ian Collins wrote:
If you want the passed object to be const, declare it const.

static void testfunc( const X* x ) {...}
What about the caller having to know all about the union's internals?

-mi

Aug 11 '06 #30

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

Similar topics

5
34388
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
3
3583
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID from letdata UNION SELECT as ID FROM MEMODATA; I get an ODBC error. The same query runs when the backend files are MDB files and it runs with MYSQL if I only combine 2 tables.
4
2946
by: shaun palmer | last post by:
when or Where do you use a union query ? how can I wright sql, tblPopulation,* tblcustomer,* one to one with all the appropriate attributes(field). Your help would be greatly appreciated. Thank you.
1
1603
by: turtle | last post by:
I need to get the results of two queries into one. I need all the data from both queries (i guess this is a full join or outer join). Can I do a union query. Query1 Job Budget Function 1 10 Electrical Engineer 1 5 Mechanical Engineer 2 20 Elecrical Engineer
2
5066
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 ?
17
2827
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
3
2229
by: mikes | last post by:
I have 2 separate queries, which effectively are the same except they draw data from separate tables. Both tables are (design-wise) identical, only the data is different. for each query, there are 2 tables with a standard LEFT JOIN. One field of the query is calculated, looking for a NULL in one table, and then using a field from the second table in that case. One query looks like this: PARAMETERS Text ( 255 ); SELECT...
1
5157
by: BerkshireGuy | last post by:
I have the following union query: SELECT Count(PolicyNumber) AS TotalSubmitted, "TOTAL SUBMITTED" as Header, "Combined" as TelemedClass, "1" as GroupA,"1" as Postion FROM tblAppActivity WHERE AppStatusSort = "1" And RDLastTransaction Between #01/01/2006# and #09/30/2006# UNION ALL SELECT Count(PolicyNumber) AS TotalTelemedSubmittedIC, "TOTAL TELEMED
29
2209
by: Richard Harter | last post by:
There is probably a simple way to do what I want but I don't see it. Any suggestions are welcome. Suppose I have a function foo with an argument that can be any of several types and that I want to use a union for that argument. In a header file there is the following: #define VAL_ALT union urt_value_alt .....
0
9845
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
11279
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
10981
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,...
1
8047
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
7205
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();...
0
5893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4306
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.