![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
? : 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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
It's equivalent to writing:
int *z,*w; z=&x; w=&y; (y>x) ? *w : *z =100;
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
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 ? |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
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;
}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. |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Apr 2006
Location: Israel
Posts: 4
Rep Power: 0
![]() |
Quote:
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 ![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|