macro function compiles error,can anybody explain why?3x!
-
Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:
#include "stdio.h"
#define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */
void main()
{
int a = -1;
int c = ABSOLUTE(1); //line 6
int b = ABSOLUTE(-1); //line 7
} -
Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:
#include "stdio.h"
#define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */
void main()
{
int a = -1;
int c = ABSOLUTE(1); //line 6
int b = ABSOLUTE(-1); //line 7
}Make it #define ABSOLUTE(a) (a > 0 ? a : -1*a) by the way there is already a macro defined as "ABSOLUTE" in wingdi.h ;)
Md. Humayuon Kabir Hemoo
-
Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:
#include "stdio.h"
#define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */
void main()
{
int a = -1;
int c = ABSOLUTE(1); //line 6
int b = ABSOLUTE(-1); //line 7
}Try using this
#define ABSOLUTE(a) (((a) > 0) ?(a) : -(a))
Do more work Make more mistakes Learn more things
-
Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:
#include "stdio.h"
#define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */
void main()
{
int a = -1;
int c = ABSOLUTE(1); //line 6
int b = ABSOLUTE(-1); //line 7
}What you have to understand about macro is that they are simply a 'text replacement': the precompiler will replace occurances of ABSOLUTE in your code by the macro definition, before the compilation. So, it means that ABSOLUTE(-1) will be replaced by:
(((-1) < 0) ? (-1) : (--1))
As you can see, the --1 is not valid. This doesn't happen for line 6 because it will be replaced by:
(((1) < 0) ? (1) : (-1))
which is valid.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ [Part 2 online] -
Make it #define ABSOLUTE(a) (a > 0 ? a : -1*a) by the way there is already a macro defined as "ABSOLUTE" in wingdi.h ;)
Md. Humayuon Kabir Hemoo
-
What you have to understand about macro is that they are simply a 'text replacement': the precompiler will replace occurances of ABSOLUTE in your code by the macro definition, before the compilation. So, it means that ABSOLUTE(-1) will be replaced by:
(((-1) < 0) ? (-1) : (--1))
As you can see, the --1 is not valid. This doesn't happen for line 6 because it will be replaced by:
(((1) < 0) ? (1) : (-1))
which is valid.
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ [Part 2 online]