Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old May 4th, 2006, 1:13 PM   #21
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
Read all the posts and you'll get the answer yourself
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 4th, 2006, 2:32 PM   #22
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
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?
leo1502 is offline   Reply With Quote
Old May 4th, 2006, 2:55 PM   #23
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
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.
andro is offline   Reply With Quote
Old May 4th, 2006, 5:53 PM   #24
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 4th, 2006, 5:53 PM   #25
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
Yeah, just give addresses for the program to assign to, and it'll work fine.
Twilight is offline   Reply With Quote
Old May 4th, 2006, 8:58 PM   #26
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old May 5th, 2006, 1:36 PM   #27
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
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)
leo1502 is offline   Reply With Quote
Old May 5th, 2006, 3:31 PM   #28
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
#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?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 5th, 2006, 4:51 PM   #29
leo1502
Newbie
 
Join Date: Apr 2006
Posts: 16
Rep Power: 0 leo1502 is on a distinguished road
#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
100

now 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?
leo1502 is offline   Reply With Quote
Old May 5th, 2006, 5:51 PM   #30
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by leo1502
[code]
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?
I thought this thread would be well and truly dead by now.

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.
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 3:05 AM.

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