Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 29th, 2006, 12:36 PM   #1
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
? : operator in c\C++ - what's goin on

I need to write a macro which changes the max value of 2 given integers using this syntax:
SmartMax(x,y)=100

I would expect that this code won't compile or run right.
the ? : operator is suppoesd to make a copy of *w or *z so there is no reason for x or y to change.

#include <stdio.h>
#define SmartMax(x,y) int *z,*w; z=&x; w=&y; (y>x) ? *w : *z

int main()
{
int x=2,y=6;
SmartMax(x,y)=100;
printf("%d\n",y);
return 0;
}


but, I found out that the code works fine when I compile using gcc
in g++ though, it doesn't do what I wanted and returns y 6

what's goin on here?
leo1502 is offline   Reply With Quote
Old Apr 29th, 2006, 1:50 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You want free advice, yet you don't feel you should read the forum's rules/FAQ to get a feel for the community you want the advice from? Shame on you. If you decide to read it, keep a sharp eye out for the part about 'code tags'.

Since a macro is a preprocessed thangy that is substituted into your source before the compiler sees it, you might hand-expand your macro into your code in the same way and see for yourself why it doesn't perform up to expectations.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 29th, 2006, 1:54 PM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
It's equivalent to writing:
int *z,*w; 
z=&x; 
w=&y; 
(y>x) ? *w : *z =100;
Can cause problems if you write the macro in the middle of the code as most of the C compilers like all the declarations to be at the start of a function.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 29th, 2006, 2:03 PM   #4
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
Thanks, but you haven't really answered my question.

I know what a macro is, that's not the problem here.
why does the code work right on C but not on C++?

looks like C returns the real value X (or Y) and not a copy of it.

About the variables in the middle of the code, is there a way to solve this problem without using new variables, yet preserving the syntax SmartMax(x,y)=100 ?
leo1502 is offline   Reply With Quote
Old Apr 29th, 2006, 2:08 PM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Since you are using gcc, you can choose to only do the preprocessing and then look at the result to see if that's what you want:
g++ -E inputfile.cpp -o outputfile.cpp
Quote:
Originally Posted by leo1502
I need to write a macro which changes the max value of 2 given integers
I'm not sure what you mean by that. What do you need to do? Find the highest value, x or y?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 29th, 2006, 2:12 PM   #6
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
How does that answer my question?
I already know it works right with gcc

by SmartMax(x,y)=10

I mean if y>x then y will be 10, else x will be 10
the greater number will change to 10
leo1502 is offline   Reply With Quote
Old Apr 29th, 2006, 4:49 PM   #7
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>How does that answer my question?
By showing you how to find the problem, genius. Did you expect us to fix all of your problems and hand you a shiny new macro that perfectly does everything you want?

>I already know it works right with gcc
I hope you didn't expect it to work right elsewhere. The result of the conditional operator can't be used as an lvalue. It's not guaranteed to work on other compilers, or even different versions of gcc.

By the way, that macro is pretty much guaranteed to be error prone and frustrating no matter how you write it. Are you trying to duplicate the effect of a C++ function returning a reference?
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 29th, 2006, 5:05 PM   #8
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
no, I didn't want you to hand me a shiny new macro.
just tried to figure out what's goin' on there.
someone told me C returns the right lvalue there when I use a dynamic var like with w and z.
is that true?

Narue, what are you so pissed about anyway
leo1502 is offline   Reply With Quote
Old Apr 29th, 2006, 5:20 PM   #9
OopSheMishtamesh
Newbie
 
Join Date: Apr 2006
Location: Israel
Posts: 4
Rep Power: 0 OopSheMishtamesh is on a distinguished road
Quote:
Originally Posted by leo1502

#define SmartMax(x,y) int *z,*w; z=&x; w=&y; (y>x) ? *w : *z

SmartMax(x,y)=100;

this is the most crappy piece of code i seen in the past month , it shouldn't compile at all ...
this makes a copy of some value and puts there 100 ..

this doesn't make any sense ..

or maybe the ? : operator doesn't make a copy , and returns the X or the Y (don't ask me how)

if the last assumption is correct, this should work too (y>x) ? y : x, if not, we have some real issues here...


cheers m8s
OopSheMishtamesh is offline   Reply With Quote
Old Apr 29th, 2006, 6:38 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by leo1502
How does that answer my question?
I already know it works right with gcc

by SmartMax(x,y)=10

I mean if y>x then y will be 10, else x will be 10
the greater number will change to 10
Well, yeah, it will work if you do other things right.

But it is also easily broken.

Try this;
int main()
{
    int x=2,y=6;
    double z;
    SmartMax(x,y)=100;
    printf("%d\n",y);
    return 0;
}
and watch your compiler complain bitterly.

Formally, the result of ?: can be an lvalue if both possible results are lvalues. You would be better off avoiding introducing the variables w and z within your macro; they actually serve no purpose and, as in my example here, open up all sorts of opportunities to write broken program.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC