#define Macros... need some help
ok I am working on a macro to call a function with multiple variables...
#define LoadIt(x,y) if (x, y) {G.back_surf[x] = bitmap_surface(y);}
x is an int variable
y is a LPSTR string...its a filename
this crashes my comp, but my bitmap surface function works fine.
i basically am using a define# from Lamothe I believe...
#define SafeRelease(x) if (x) {x->Release(); x=NULL;}
i figured i could modify it to do other stuff.
any ideas on how to get my #define to work? I probably just don't understand defines very well and am doing something totally wrong with the variables.
Thanks,
MikeD
Edited by - darkpunk on May 7, 2001 12:48:19 AM
Edited by - darkpunk on May 7, 2001 12:49:50 AM
the if(x,y) looks suspicious since that is not proper grammer
I believe you want
#define LoadIt(x,y) if(x){G.back_surf[x] = bitmap_surface(y);}
Just a thought.
I believe you want
#define LoadIt(x,y) if(x){G.back_surf[x] = bitmap_surface(y);}
Just a thought.
cool thanks I will try that. yeah i guess your right. haha if statements dont take variables like that.
The ![](wink.gif)
Edited by - merlin9x9 on May 8, 2001 2:13:44 PM
if(x, y)
is correct grammar; it just doesn't do what he wants it to do. ![](wink.gif)
Edited by - merlin9x9 on May 8, 2001 2:13:44 PM
Erm... Are you sure if(x, y) is correct grammer?? 'if' statement is followed by a condition, which evaluates to either zero or non-zero. (x, y) as a statement doesn't seem to make sense... It looks more like someone tried to declare a function called if(x,y).
If it is valid grammer, then how is (x, y) evaluated as a condition? Do both x and y have to be non-zero to fullfill the condition?
if(x) makes sense, however, as if x can either be zero or non-zero. The line
#define SafeRelease(x) if (x) {x->Release(); x=NULL;}
also makes sense. x must be a surface or some DirectX pointer. It checks to see if the pointer x is not NULL by using if(x). Then releases it and sets it to NULL. If it's already NULL, if(x) is basically if(0) so the following statement is ignored.
darkpunk, you don't explain what you want to do there. But from the look of it, I guess you are trying to check and see if the filename pointer y is valid with if statement... unless you have a reason to check the x as well. Try the following:
#define LoadIt(x,y) if(y){G.back_surf[x] = bitmap_surface(y);}
Or, if you are trying to check and see if BOTH x and y are non-zero... Never tried that in a macro, but I guess nesting if statements will work....
#define LoadIt(x,y) if(x){if(y){G.back_surf[x] = bitmap_surface(y);}}
This sort of macro is dodgy though... you never know how you will end up using LoadIt() macro, and how it will work out when you slot this into an obscure line... I'd use a proper function if I were you, that is:
BOOL LoadIt( int x, LPSTR y )
{
if ( y == NULL ) return FALSE;
G.back_surf[x] = bitmap_surface(y);
return TRUE;
}
Don't forget to declare it in a header file, whatever you are calling it (ends with .h extention).
One final thing. Make sure bitmap_surface(y) is returning the data type which is appropriate for the surface (VC++ returns an error if you are doing this anyway). Also you say that it crashes. It may be that you are using LoadIt() in a loop, and incrementing x? or something. Make sure you aren't overshooting the surface buffer. You might be trying to write outside the back_surf buffer, in which case it may very well crash.
Either way, we don't understand what you are trying to do there so it's hard to be specific on helping you out.
Edited by - PugPenguin on May 8, 2001 8:17:29 PM
If it is valid grammer, then how is (x, y) evaluated as a condition? Do both x and y have to be non-zero to fullfill the condition?
if(x) makes sense, however, as if x can either be zero or non-zero. The line
#define SafeRelease(x) if (x) {x->Release(); x=NULL;}
also makes sense. x must be a surface or some DirectX pointer. It checks to see if the pointer x is not NULL by using if(x). Then releases it and sets it to NULL. If it's already NULL, if(x) is basically if(0) so the following statement is ignored.
darkpunk, you don't explain what you want to do there. But from the look of it, I guess you are trying to check and see if the filename pointer y is valid with if statement... unless you have a reason to check the x as well. Try the following:
#define LoadIt(x,y) if(y){G.back_surf[x] = bitmap_surface(y);}
Or, if you are trying to check and see if BOTH x and y are non-zero... Never tried that in a macro, but I guess nesting if statements will work....
#define LoadIt(x,y) if(x){if(y){G.back_surf[x] = bitmap_surface(y);}}
This sort of macro is dodgy though... you never know how you will end up using LoadIt() macro, and how it will work out when you slot this into an obscure line... I'd use a proper function if I were you, that is:
BOOL LoadIt( int x, LPSTR y )
{
if ( y == NULL ) return FALSE;
G.back_surf[x] = bitmap_surface(y);
return TRUE;
}
Don't forget to declare it in a header file, whatever you are calling it (ends with .h extention).
One final thing. Make sure bitmap_surface(y) is returning the data type which is appropriate for the surface (VC++ returns an error if you are doing this anyway). Also you say that it crashes. It may be that you are using LoadIt() in a loop, and incrementing x? or something. Make sure you aren't overshooting the surface buffer. You might be trying to write outside the back_surf buffer, in which case it may very well crash.
Either way, we don't understand what you are trying to do there so it's hard to be specific on helping you out.
Edited by - PugPenguin on May 8, 2001 8:17:29 PM
quote:
Original post by PugPenguin
Erm... Are you sure if(x, y) is correct grammer?? ''if'' statement is followed by a condition, which evaluates to either zero or non-zero. (x, y) as a statement doesn''t seem to make sense... It looks more like someone tried to declare a function called if(x,y).
If it is valid grammer, then how is (x, y) evaluated as a condition? Do both x and y have to be non-zero to fullfill the condition?
This is an example of the comma operator in C. It evaluates to the rightmost argument (in this case, y). The wonderful thing about the comma operator is you can write hideously confusing code such as (i++, --j, x == y, 7) which, naturally always evaluates to 7 but can be put anywhere you can put an expression.
Ohhh so (x, y) evaluates? like if I do
int b = 1;
int a = (b++, 0);
then a = 0 and b = 2? I never knew that! Gotta love these boards, all these pearls of wisdom![](smile.gif)
One last question is, what is the order of evaluation? right to left as bog standard? For example:
int a = 0;
a = (++a, a);
then would a be 0 or 1?
Thanks for input anyway Jaxon, I wouldn''t have known this comma operator without you. It''s a very nice trick. Can you think of any other "obscure" wisdom which is rarely used or often forgotten in C? I might know but doesn''t matter, I might pick up a thing or two.
int b = 1;
int a = (b++, 0);
then a = 0 and b = 2? I never knew that! Gotta love these boards, all these pearls of wisdom
![](smile.gif)
One last question is, what is the order of evaluation? right to left as bog standard? For example:
int a = 0;
a = (++a, a);
then would a be 0 or 1?
Thanks for input anyway Jaxon, I wouldn''t have known this comma operator without you. It''s a very nice trick. Can you think of any other "obscure" wisdom which is rarely used or often forgotten in C? I might know but doesn''t matter, I might pick up a thing or two.
In that example, a ends up being 1.
Somebody mentioned around here not too long ago that since addition is commutative and since array indices are represented as offsets (additions) to the base address of the array, these lines of code are 100% functionally equivalent and valid syntax:
Somebody mentioned around here not too long ago that since addition is commutative and since array indices are represented as offsets (additions) to the base address of the array, these lines of code are 100% functionally equivalent and valid syntax:
x = array[100]; // becomes x = *(array + 100)x = [100]array; // becomes x = *(100 + array)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement