Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > union, ternary operator, and C. What a mess!

Reply
Thread Tools

union, ternary operator, and C. What a mess!

 
 
Paul E Johnson
Guest
Posts: n/a
 
      10-17-2003
Dear friends in C:

I'm completely baffled by this problem I have with
printf statements and conditional operators in C.
I'm using gcc-3.3.

I have a project where the rarely used Union type
is called for! Incredible, but true.

union mixed
{
long unsigned int i;
double f;
} newMem;


It is no trouble to retrieve the values with newMem.i or newMem.f.
However, I want to make the choice between the two automatic by writing a
macro like this, which keys on the value of a variable "Stochastic":

#define UMACRO(var) ((Stochastic) ? var.i : var.f )

So if Stochastic ==1 then I want to get back newMem.i,
otherwise I want newMem.f.

I get this psychotic behavior. WHen the macro UMACRO is in a
printf statement, things go completely berserk!

Here's a code snip:

printf("newMem = %lu \n", newMem.i);
printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
UMACRO(newMem), Stochastic);
{
long unsigned int testval = UMACRO(newMem);
printf("testval %lu\n", testval);
}


And here's the output it produces when Stochastic==1 and
newMem.i = 97:

newMem = 97
Stochastic=1, UMACRO = 0 Stochastic = 1079525376
testval 97

When you look at the ouptut in line 2, you figure UMACRO
does not work at all, and it does something horrible affect
the printout of the second Stochastic in that line. However,
note the 3rd line, which is produced by first retrieving the value from
the union and then printing it out. That is correct.


I was thinking that the problem is the pre-processor, but
here is the output from gcc -E, which appears as expected.

printf("newMem = %lu \n", newMem.i);
printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
((Stochastic)?newMem.i:newMem.f ), Stochastic);
{long unsigned int testval = ((Stochastic)?newMem.i:newMem.f );
printf("testval %lu\n", testval);}

So maybe the ternary conditional is not working in the
right hand side of the printf? Is that a known thing? And
why does it make the output of the second Stochastic in line 2 turn into
crapola 1079525376.

???

--
Paul E. Johnson email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn
 
Reply With Quote
 
 
 
 
xarax
Guest
Posts: n/a
 
      10-17-2003
"Paul E Johnson" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Dear friends in C:
>
> I'm completely baffled by this problem I have with
> printf statements and conditional operators in C.
> I'm using gcc-3.3.
>
> I have a project where the rarely used Union type
> is called for! Incredible, but true.
>
> union mixed
> {
> long unsigned int i;
> double f;
> } newMem;
>
>
> It is no trouble to retrieve the values with newMem.i or newMem.f.
> However, I want to make the choice between the two automatic by writing a
> macro like this, which keys on the value of a variable "Stochastic":
>
> #define UMACRO(var) ((Stochastic) ? var.i : var.f )
>
> So if Stochastic ==1 then I want to get back newMem.i,
> otherwise I want newMem.f.
>
> I get this psychotic behavior. WHen the macro UMACRO is in a
> printf statement, things go completely berserk!
>
> Here's a code snip:
>
> printf("newMem = %lu \n", newMem.i);
> printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
> UMACRO(newMem), Stochastic);
> {
> long unsigned int testval = UMACRO(newMem);
> printf("testval %lu\n", testval);
> }
>
>
> And here's the output it produces when Stochastic==1 and
> newMem.i = 97:
>
> newMem = 97
> Stochastic=1, UMACRO = 0 Stochastic = 1079525376
> testval 97
>
> When you look at the ouptut in line 2, you figure UMACRO
> does not work at all, and it does something horrible affect
> the printout of the second Stochastic in that line. However,
> note the 3rd line, which is produced by first retrieving the value from
> the union and then printing it out. That is correct.
>
>
> I was thinking that the problem is the pre-processor, but
> here is the output from gcc -E, which appears as expected.
>
> printf("newMem = %lu \n", newMem.i);
> printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
> ((Stochastic)?newMem.i:newMem.f ), Stochastic);
> {long unsigned int testval = ((Stochastic)?newMem.i:newMem.f );
> printf("testval %lu\n", testval);}
>
> So maybe the ternary conditional is not working in the
> right hand side of the printf? Is that a known thing? And
> why does it make the output of the second Stochastic in line 2 turn into
> crapola 1079525376.


This is likely caused by the different sizes of
"long unsigned int" vs. "double".

I suggest moving the (Stochastic)? test to an if(Stochastic) statement.
Then use hard-coded references to newMem.i or newMem.f in the
appropriate "then" or "else" part of the if() statement.

if(Stochastic)
{
printf(..whatever..,newMem.i);
}
else
{
printf(..whatever..,newMem.f);
}

HTH


 
Reply With Quote
 
 
 
 
Ed Morton
Guest
Posts: n/a
 
      10-17-2003


Paul E Johnson wrote:

> Dear friends in C:
>
> I'm completely baffled by this problem I have with
> printf statements and conditional operators in C.
> I'm using gcc-3.3.
>
> I have a project where the rarely used Union type
> is called for! Incredible, but true.


Rarely used? I use them all the time...

<snip>


> I was thinking that the problem is the pre-processor, but
> here is the output from gcc -E, which appears as expected.


Didn't it also give you a warning about the third printf? If not, try it
again with "-Wall" set.

> printf("newMem = %lu \n", newMem.i);
> printf("Stochastic=%d, UMACRO = %lu Stochastic = %d \n",Stochastic,
> ((Stochastic)?newMem.i:newMem.f ), Stochastic);
> {long unsigned int testval = ((Stochastic)?newMem.i:newMem.f );
> printf("testval %lu\n", testval);}
>
> So maybe the ternary conditional is not working in the
> right hand side of the printf? Is that a known thing? And
> why does it make the output of the second Stochastic in line 2 turn into
> crapola 1079525376.
>
> ???
>


Your macro is fine, it's your printf fomatting that's bad. Try using %e
(for "double") instead of %lu (for "long...") in UMACRO= part and you'll
see the difference.

Ed.

 
Reply With Quote
 
Ed Morton
Guest
Posts: n/a
 
      10-17-2003


Ed Morton wrote:


> Didn't it also give you a warning about the third printf?

s/third/second/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using ternary and summing array flebber C Programming 11 11-21-2011 07:39 AM
Ternary operator and tuple unpacking -- What am I missing ? imageguy Python 7 01-13-2009 09:58 PM
Ternary operator and memory access Udo A. Steinberg C Programming 20 08-08-2006 12:57 AM
generator expressions and new ternary operator zipher Python 2 09-13-2004 07:02 AM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM



Advertisments