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

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 #1
50 6394
Mikhail Teterin said:
Hello!

The sample program below is compiled fine by gcc (with -Wall), but
rejected by Sun's SUNWspro compiler (version 6 update 2).
What sample program?
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.
The value of the most recently assigned union member object can be passed to
a function directly. There is no need to copy it out.
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!
Who knows? I don't see your code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 11 '06 #2
Richard Heathfield wrote:
What sample program?
Your news-reader must be doing something nasty with attachments.

The program was attached to the original posting. Here it is inline.

-mi

#include <stdio.h>

typedef union {
ššššššššintššššši;
ššššššššvoidšššš*p;
ššššššššstruct {
ššššššššššššššššintššššši;
ššššššššššššššššintšššššj;
šššššššš}šššššššs;
} testunion;

static void
testfunc(testunion u)
{
ššššššššprintf("i: %d\np: %p\n", u.i, u.p);
}

int
main()
{
šššššššštestfunc((testunion)3);
šššššššštestfunc((testunion)NULL);
ššššššššreturn 0;
}
Aug 11 '06 #3
Mikhail Teterin said:
Richard Heathfield wrote:
>What sample program?

Your news-reader must be doing something nasty with attachments.
Attachments don't happen in comp.lang.c - it's a text-only newsgroup.
#include <stdio.h>

typedef union {
int i;
void *p;
struct {
int i;
int j;
} s;
} testunion;

static void
testfunc(testunion u)
{
printf("i: %d\np: %p\n", u.i, u.p);
Either you put in an int, in which case it's okay to print the int but not
the void *, or you put in a void *, in which case it's okay to print the
void * but not the int.
}

int
main()
{
testfunc((testunion)3);
That's not a valid conversion.
testfunc((testunion)NULL);
Neither is that.
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 11 '06 #4
Richard Heathfield wrote:
Attachments don't happen in comp.lang.c - it's a text-only newsgroup.
Mine was a text-only attachement. And I can see it on my news-server...
Mikhail Teterin said:
>testfunc((testunion)3);

That's not a valid conversion.
But gcc has no problem with it, and my question was: "why?" Is gcc too lax,
or is it aware of a newer C-standard that neither then Sun's compiler nor
you are aware of?

If a function expects a union, one of whose fields is `int':

typedef union {
int i;
void *p;
struct {
int i;
int j;
} s;
} testunion;

then passing an integer argument (with or without casting) is quite
unambigious -- gcc's treatment seems perfectly reasonable to me...

-mi
Aug 11 '06 #5
Mikhail Teterin <us****@aldan.algebra.comwrites:
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!

#include <stdio.h>

typedef union {
int i;
void *p;
struct {
int i;
int j;
} s;
} testunion;

static void
testfunc(testunion u)
{
printf("i: %d\np: %p\n", u.i, u.p);
}

int
main()
{
testfunc((testunion)3);
testfunc((testunion)NULL);
return 0;
}
Your program appeared in my newsreader as an attachment. Please don't
post attachments here; just copy your source code into the text of
your article.

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.

--
Keith Thompson (The_Other_Keith) 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 #6
Mikhail Teterin wrote:
Richard Heathfield wrote:

>>Attachments don't happen in comp.lang.c - it's a text-only newsgroup.


Mine was a text-only attachement. And I can see it on my news-server...
Doesn't matter, text-only newsgroups don't forward attachments.
>
>>Mikhail Teterin said:
>>>testfunc((testunion)3);

That's not a valid conversion.


But gcc has no problem with it, and my question was: "why?" Is gcc too lax,
or is it aware of a newer C-standard that neither then Sun's compiler nor
you are aware of?
In default mode, gcc will compile just about anything remotely like C.

gcc -Wall -pedantic -ansi /tmp/x.c
/tmp/x.c: In function `main':
/tmp/x.c:21: warning: ISO C forbids casts to union type
/tmp/x.c:22: warning: ISO C forbids casts to union type

Compile with an appropriate warning level, you'll learn more that way.

--
Ian Collins.
Aug 11 '06 #7
Ian Collins wrote:
Mikhail Teterin wrote:
>>Richard Heathfield wrote:
>>>Attachments don't happen in comp.lang.c - it's a text-only newsgroup.


Mine was a text-only attachement. And I can see it on my news-server...

Doesn't matter, text-only newsgroups don't forward attachments.
Oops, got that one wrong. It did....

--
Ian Collins.
Aug 11 '06 #8
Mikhail Teterin <us****@aldan.algebra.comwrites:
Richard Heathfield wrote:
>What sample program?

Your news-reader must be doing something nasty with attachments.

The program was attached to the original posting. Here it is inline.
Ugh, that's worse. In my newsreader, it shows up with a bunch of
non-printable '\232' characters.

I don't know how they got there, but try to use only spaces for
indentation. Copy and paste plain text if you can.

--
Keith Thompson (The_Other_Keith) 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 #9
Ian Collins wrote:
In default mode, gcc will compile just about anything remotely like C.
I'm compiled with -Wall, not default, although I don't use -pedantic.
gcc -Wall -pedantic -ansi /tmp/x.c
/tmp/x.c: In function `main':
/tmp/x.c:21: warning: ISO C forbids casts to union type
/tmp/x.c:22: warning: ISO C forbids casts to union type

Compile with an appropriate warning level, you'll learn more that way.
With -Wall, but without -pedantic, the code still compiles cleanly and works
as expected:

% gcc -Wall -o union union.c
% ./union
i: 3
p: 3
i: 0
p: 0

Adding "-pedantic" does, indeed, elicit warnings you quote. So, is there a
way to pass a value to a union-expecting function, without creating a union
and copying the values around, as in:

testunion t;

t.i = i;
testfunc(t);

Why can't I just do:

testfunc(i)

? Is there any kind of ambiguity here?

The reason, I'm so insistant is not only having to copy values around and
create (seemingly) useless local variables. It is also that Purify reports
such passing of unions as a UMR (Uninitialized Memory Read), because other
(longer) fields of the union remain unitilized (such as the s.j field in my
sample union) and putting them onto stack (for a function call)
means "reading" them...

Thanks,

-mi
Aug 11 '06 #10
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...
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 :-)

http://www.freebsd.org/cgi/man.cgi?q...tyle&sektion=9

Yours,

-mi
Aug 11 '06 #11
Keith Thompson 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...
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,

-mi
Aug 11 '06 #12
Mikhail Teterin wrote:
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...
Well, your "inline" version looks like crap on Google Groups as well,
take a look for yourself if you'd like.
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 :-)

http://www.freebsd.org/cgi/man.cgi?q...tyle&sektion=9
Perhaps you don't realize it but this is Usenet, not the BSD kernel.
Spaces are the recommended method of indentation here and for good
reason.

Robert Gamble

Aug 11 '06 #13
Mikhail Teterin wrote:
>
Adding "-pedantic" does, indeed, elicit warnings you quote. So, is there a
way to pass a value to a union-expecting function, without creating a union
and copying the values around, as in:

testunion t;

t.i = i;
testfunc(t);

Why can't I just do:

testfunc(i)

? Is there any kind of ambiguity here?
Think of your union as an object. If you had written

typedef struct { int n; } X;

void testFn( X x ) {}

You wouldn't expect to be able to pass an int to testFn, same when the
parameter is a union.
The reason, I'm so insistant is not only having to copy values around and
create (seemingly) useless local variables.
Why worry? Let the compiler take care of optimising away local variables.
It is also that Purify reports
such passing of unions as a UMR (Uninitialized Memory Read), because other
(longer) fields of the union remain unitilized (such as the s.j field in my
sample union) and putting them onto stack (for a function call)
means "reading" them...
What else did you expect?

Using unions as function parameters is not a good idea in my opinion.
If you do, I'd recommend wrapping them in a simple struct with a member
used to indicate which member of the union is being passed:

typedef union {
int i;
void *p;
struct {
int i;
int j;
} s;
} testunion;
typedef struct {
enum { Int, Void, Struct} key;
testunion value;
} X;

static void testfunc( X* x ){}

int main(void)
{
X x;
x.key = Int;
x.value.i = 42;

testfunc( &x );

return 0;
}

--
Ian Collins.
Aug 11 '06 #14
Mikhail Teterin wrote:
Keith Thompson 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.

--
Ian Collins.
Aug 11 '06 #15
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.

A text-only attachement is no different, really from a "--- cut here ---" --
look at the "body" of my original message to see, what I mean. It is not
encoded, and the reader is not expected to render any images. One could
telnet to port 119, speak NNTP to get the article and would *still* get the
posted textual attachment correctly...

-mi

Aug 11 '06 #16
Robert Gamble wrote:
Perhaps you don't realize it but this is Usenet, not the BSD kernel.
Spaces are the recommended method of indentation here and for good
reason.
The reason being?..

-mi
Aug 11 '06 #17
Mikhail Teterin <us****@aldan.algebra.comwrites:
Keith Thompson 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...
No, it isn't. At least one responder said his newsreader didn't even
show him the attachment. Each newsreader handles attachments in its
own way, and they're strongly discouraged in discussion groups.

The "--- cut here ---" stuff is easy enough to handle.
>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?
There are an unlimited number of things that could be added to the
standard. If they were all added, a copy of the standard would be too
heavy to lift. New features shouldn't be added to the core language
just for the sake of a minor convenience.

--
Keith Thompson (The_Other_Keith) 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 #18
Ian Collins wrote:
>? Is there any kind of ambiguity here?
Think of your union as an object. If you had written

typedef struct { int n; } X;

void testFn( X x ) {}

You wouldn't expect to be able to pass an int to testFn, same when the
parameter is a union.
But I don't think of it as an object -- not in this case. It is just a way
to pass a different type of argument to the same function (without the mess
of varargs/stdargs).
>The reason, I'm so insistant is not only having to copy values around and
create (seemingly) useless local variables.

Why worry? Let the compiler take care of optimising away local variables.
The code looks ugly -- with many extra lines. It also requires the caller to
know about the union's internals, when all it needs to know, is that it
passes a number.
>It is also that Purify reports such passing of unions as a UMR
(Uninitialized Memory Read), because other (longer) fields of the union
remain unitilized (such as the s.j field in my sample union) and putting
them onto stack (for a function call) means "reading" them...
What else did you expect?
Well, I expect Purify to tell me about *real* problems in the code... Having
been alerted to this by Purify, I tried to come up with a better
replacement for the alarm-triggering code...
Using unions as function parameters is not a good idea in my opinion.
If you do, I'd recommend wrapping them in a simple struct with a member
used to indicate which member of the union is being passed.
In my case the type is specified by an earlier argument (stripped away for
the sample program posted). Same thing, really.
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...

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

-mi
Aug 11 '06 #19
Mikhail Teterin <us****@aldan.algebra.comwrites:
Robert Gamble wrote:
>Perhaps you don't realize it but this is Usenet, not the BSD kernel.
Spaces are the recommended method of indentation here and for good
reason.

The reason being?..
tabs can be messed up or lost in transit. I have no idea how or why,
but it happens; someone posts this:

<TAB>{
<TAB><TAB>/* indented text */
<TAB>}

and it shows up like this:

{
/* indented text*/
}

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

--
Keith Thompson (The_Other_Keith) 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 #20
Mikhail Teterin <us****@aldan.algebra.comwrites:
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_Keith) 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.algebra.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
Mikhail Teterin wrote:
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?
What about it? No worse than having ton know about the type being passed.

--
Ian Collins.
Aug 11 '06 #31
Mikhail Teterin said:
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 :-)
Did my second response not count as actual advice on the subject matter?
Hmm.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 11 '06 #32
Ian Collins said:
Al Balmer wrote:
>Here's what I got:

typedef union {
<non-printable stuff snipped>
>} testunion;
Just curious, which news reader?
According to his headers, he's using Forte Agent 3.3/32.846.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 11 '06 #33
Ian Collins wrote:
>>>static void testfunc( const X* x ) {...}


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.
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
Aug 11 '06 #34
Mikhail Teterin wrote:
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 :-)
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.

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

>>>>static void testfunc( const X* x ) {...}
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.


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.

--
Ian Collins.
Aug 11 '06 #36
Mikhail Teterin <us****@aldan.algebra.comwrites:
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...
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.

--
Keith Thompson (The_Other_Keith) 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 #37
Mikhail Teterin wrote:
Robert Gamble wrote:
Perhaps you don't realize it but this is Usenet, not the BSD kernel.
Spaces are the recommended method of indentation here and for good
reason.

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

Aug 11 '06 #38
On Fri, 11 Aug 2006 23:13:21 +0000, Richard Heathfield
<in*****@invalid.invalidwrote:
>Ian Collins said:
>Al Balmer wrote:
>>Here's what I got:

typedef union {

<non-printable stuff snipped>
>>} testunion;
Just curious, which news reader?

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.

--
Al Balmer
Sun City, AZ
Aug 11 '06 #39
On Fri, 11 Aug 2006 17:59:42 -0400, Mikhail Teterin
<us****@aldan.algebra.comwrote:
>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.

--
Al Balmer
Sun City, AZ
Aug 11 '06 #40
Keith Thompson wrote:
The program was attached to the original posting. Here it is inline.

Ugh, that's worse. In my newsreader, it shows up with a bunch of
non-printable '\232' characters.
That is 'no-break space' (&nbsp;) in the koi8-r charset.
That is probably what you get for spaces when you try to copy'n'paste
from
one of those fancy newsreaders :)
I don't know how they got there, but try to use only spaces for
indentation. Copy and paste plain text if you can.
Aug 11 '06 #41
On Fri, 11 Aug 2006 18:44:47 -0400, Mikhail Teterin
<us****@aldan.algebra.comwrote:
>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.
I've never seen a newsreader that couldn't be told to use a fixed
font. Care to name a couple of the many? I have seen newsreaders that
didn't provide proportional fonts (but not for a long time.)
>When in Rome ...

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

-mi
--
Al Balmer
Sun City, AZ
Aug 11 '06 #42
Richard Heathfield schrieb:
Mikhail Teterin said:
>>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 :-)

Did my second response not count as actual advice on the subject matter?
Hmm.
Well, at least the OP responded to yours... ;-)

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 12 '06 #43
In article <14456249.nrYsuYhnoW@mishaMikhail Teterin <us****@aldan.algebra.comwrites:
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...
The code \232 is apparently an additional space in the Koi-8 codes
that also implement line drawing symbols. A Cyrillic space? Not
everybody is equiped with newsreaders that understand those Koi-8
codes, and for most of them is \232 is a code that they do not
understand (it is a control code in all versions of ISO 8859, and
in standard Koi-8).
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 12 '06 #44
In article <4390853.PoEsFBCd7e@mishaMikhail Teterin <us****@aldan.algebra.comwrites:
Robert Gamble wrote:
Perhaps you don't realize it but this is Usenet, not the BSD kernel.
Spaces are the recommended method of indentation here and for good
reason.

The reason being?..
That not everybody has the same tab settings as you.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 12 '06 #45
In article <4k************@individual.netMichael Mair <Mi**********@invalid.invalidwrites:
Mikhail Teterin schrieb:
....
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 theory all four may differ, but I have never encountered a system
where that was the case. But I have encountered systems where the
first three were different, while the size of the first and the
third was the same. Different systems. On one, 0.0F was not all bits
0, on the other (void *)0 was not all bits 0.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 12 '06 #46
In article <10712129.IhiqIXrjhx@mishaMikhail Teterin <us****@aldan.algebra.comwrites:
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.
Well, the news-reader I am using would not recognise an attachment when you
waved it in front of its eyes. Consider, it is just piping the article
contents through "more". (Not really, but it is very similar.)
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 13 '06 #47
In article <2d********************************@4ax.comal****************@att.net writes:
On Fri, 11 Aug 2006 18:44:47 -0400, Mikhail Teterin
<us****@aldan.algebra.comwrote:
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.
I've never seen a newsreader that couldn't be told to use a fixed
font. Care to name a couple of the many? I have seen newsreaders that
didn't provide proportional fonts (but not for a long time.)
My newsreader is using the font that the window is using where it displays
the articles. In this case just the window of a telnet session, but I
could have told the telnet session to use a proportional font.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 13 '06 #48
In article <ln************@nuthaus.mib.orgKeith Thompson <ks***@mib.orgwrites:
Mikhail Teterin <us****@aldan.algebra.comwrites:
Keith Thompson wrote:
Apparently not all newsreaders handle attachments sanely. šThey do all
handle plain text sanely.
....
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.
Nope, he is just using a space that is present in the Koi-8 encoding,
instead of the standard space.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 13 '06 #49
Dik T. Winter schrieb:
In article <4k************@individual.netMichael Mair <Mi**********@invalid.invalidwrites:
Mikhail Teterin schrieb:
...
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 theory all four may differ,
Argh -- of course I meant that the first three are more likely to
differ... *sigh*
Thank you for making that clearer.
but I have never encountered a system
where that was the case. But I have encountered systems where the
first three were different, while the size of the first and the
third was the same. Different systems. On one, 0.0F was not all bits
0, on the other (void *)0 was not all bits 0.
The former, 0.0F not being all bits zero, I have also encountered.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Aug 13 '06 #50

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

Similar topics

5
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...
3
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 ...
4
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...
1
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 ...
2
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...
17
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...
3
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...
1
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...
29
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.