passing a union's field to a function

K

Keith Thompson

Mikhail Teterin said:
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 ...
 
M

Michael Mair

Mikhail said:
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
 
M

Mikhail Teterin

Keith said:
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
 
M

Mikhail Teterin

Keith said:
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
 
I

Ian Collins

Mikhail said:
Keith Thompson wrote:




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.
 
I

Ian Collins

Mikhail said:
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.
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?
 
A

Al Balmer

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;
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.
 
I

Ian Collins

Al said:
Here's what I got:

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

Mikhail Teterin

Ian said:
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
 
M

Mikhail Teterin

Ian said:
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
 
I

Ian Collins

Mikhail said:
Ian Collins wrote:




What about the caller having to know all about the union's internals?
What about it? No worse than having ton know about the type being passed.
 
R

Richard Heathfield

Mikhail Teterin said:
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 :)

Did my second response not count as actual advice on the subject matter?
Hmm.
 
M

Mikhail Teterin

Ian said:
What about it?šNo worse than having ton know about the type being passed.

When all I'm passing is one of the fields, I don't need to know about others
(I do currently to keep Purify happy), nor even the name of the field I'm
actually passing:

testfunc(Int, 32);

does not change, when/if the union changes...

-mi
 
I

Ian Collins

Mikhail said:
Ian Collins wrote:




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 :)
I think you have answered your own question, others who may have helped
either couldn't read or couldn't be bothered to read your attachment.
Mozilla included it inline, so I didn't even notice there was an attachment.
 
I

Ian Collins

Mikhail said:
Ian Collins wrote:




When all I'm passing is one of the fields, I don't need to know about others
(I do currently to keep Purify happy), nor even the name of the field I'm
actually passing:

testfunc(Int, 32);

does not change, when/if the union changes...
Again, this my be true for this simple contrived case, but not for
something more complex. Either way, you have to pass both data and
meta-data.

As you are so keen on quoting BSD stricture, have a look at the X Event
object and related code to see how this is done in a real application.
 
K

Keith Thompson

Mikhail Teterin said:
Actually, they don't -- many use proportional fonts, which is rather a
nuissance, when it comes to software code.


:) Atilla never knew that one, I'm sure...

So you're Atilla in this scenario? I don't think that helps your
case.

Incidentally, your followup added a '\232' character before the word
"They" in the text that you quoted (I've kept it in this followup).
You might want to find out why your newsreader, or maybe your server,
is doing this, and persuade it to stop.
 
R

Robert Gamble

Mikhail said:
The reason being?..

The reason being that they often don't get translated as the poster
intended them to; for example, the default indentation level for tab
characters on many newsreaders is zero spaces.

Robert Gamble
 
A

Al Balmer

Ian Collins said:


According to his headers, he's using Forte Agent 3.3/32.846.

Yep. I don't know whether the interpretation of whatever he sent (it
obviously wasn't spaces) was due to the reader, its configuration, or
one of the several other entities the message passed through on its
way here.

Incidentally, I ignore attachments as a matter of course. I don't even
bother to click on them to verify whether they might really be text or
not. I won't configure a reader to launch attachments automatically,
either.
 
A

Al Balmer

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,

If you really want to discuss this, go to comp.std.c. That's what they
do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top