![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Read all the posts and you'll get the answer yourself
![]()
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#22 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
you're right, my mistake
#include <stdio.h>
#define SmartMax(x,y) (y>x) ? y : x
int main()
{
int x=8,y=6;
SmartMax(x,y)=100;
printf("%d\n",x);
return 0;
}> gcc -x c a.c a.c: In function `main': a.c:7: invalid lvalue in assignment here X and Y are lvalues, what am I missing here? |
|
|
|
|
|
#23 |
|
Professional Programmer
|
You're not returning a pointer to x, you're returning the value of x (I guess?)
To demonstrate, do: printf("%d\n", SmartMax(x, y)); You're basically saying "8 = 100", which is why you get the error. |
|
|
|
|
|
#24 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The trail to the answer (putting the expanded code into place in the source) was provided in post #2. Actual specific answers have occurred throughout the thread. If a person wants to try to set the value, 3 (or 'true', or 'yummy'), to 100, I doubt it's going to turn the world's math departments upside down. In some cases explanations are worthless and a strait-jacket is called for.
__________________
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 |
|
|
|
|
|
#25 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
Yeah, just give addresses for the program to assign to, and it'll work fine.
|
|
|
|
|
|
#26 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Or just write it as a function
void SmartMax(int &x, int &y, int val)
{
if (x > y)
x = val;
else
y = val;
}On any modern compiler, this would probably be inlined (or you could tell the compiler to iniline it if you want) and would produce about the same amount of code as the macro version. |
|
|
|
|
|
#27 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
You mean I should return a pointer and then do *SmartMax(x,y)=100 ?
If so, that's not what I wanted, I need the syntax to be SmartMax(x,y)=100 Or maybe I didn't follow you right, can you give an example then? about the function idea, I need it to be a macro (that's the assignment's requirements) |
|
|
|
|
|
#28 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
#include <stdio.h>
#define SmartMax(x,y) (y>x) ? y : x
int main()
{
int x=8,y=6;
SmartMax(x,y)=100;
printf("%d\n",x);
return 0;
}Let's expand this: #include <stdio.h>
int main()
{
int x=8,y=6;
(y>x) ? y : x=100;
printf("%d\n",x);
return 0;
}That look legit to you? |
|
|
|
|
|
#29 |
|
Newbie
Join Date: Apr 2006
Posts: 16
Rep Power: 0
![]() |
#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;
}
> g++ a.cpp
> ./a.out
100now it works but why does it work here and not when I use the macro: #define SmartMax(x,y) ((y>x) ? (y) :(x)) aren't x and y lvalues? shouldn't both macros return the same? can you show me how to write this macro without using new vars? |
|
|
|
|
|
#30 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Quote:
This last form should work as you intend. If it is not, you are doing something else wrong. In particular, if you take out one of the brackets or leave in a semi-colon, it will not work. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|