Creating pointer using #define
Is it possible to create pointer using #define
?
#define *HEY
I'll u开发者_运维百科se it for something like this:
#define *HEY 2
...
int *s=HEY;
No, asterisks aren't allowed in #define
macro names.
Your example variable declaration wouldn't work anyway, because it doesn't contain the text *HEY
, it only contains HEY
, which doesn't match the defined macro name.
What's that code intended to do? Make a pointer that points to the number 2?
defines are based completely on text replacement. They can only insert text everywhere you call the name. In your question you already made two mistakes.
You may not use * in the name of the macro if you want to call a macro you have to call it with the same name you defined it if * would have been legal, you sould also have called it with exactly that name *HEY
By the way. There is absolutely no secret functionality hidden somewhere in macro definitions.
#define HEY 2
simply replayces every HEY in your sourcecode with a 2 before compiling. Nothing else happens. You can replace everything you want to replace, even single opening braces if you want to.
#define OPEN (
...
cout << 2 * OPEN 3 + 4 ) << endl;
should work, even if is pretty useless
精彩评论