问题描述
我只是想知道是否有可能引用#define常量的指针。如果是,该怎么办?
I was just curious to know if it is possible to have a pointer referring to #define constant. If yes, how to do it?
推荐答案
#define
指令是的指令,这意味着预处理器会在编译任何内容之前调用它。
The #define
directive is a directive to the preprocessor, meaning that it is invoked by the preprocessor before anything is even compiled.
因此,如果您输入:
#define NUMBER 100
然后稍后键入:
int x = NUMBER;
您的编译器实际看到的内容就是:
What your compiler actually sees is simply:
int x = 100;
基本上就像您在字处理器中打开源代码一样并进行查找/替换,将每次出现的 NUMBER替换为 100。因此,您的编译器不知道 NUMBER
的存在。只有预编译预处理器知道 NUMBER
的含义。
It's basically as if you had opened up your source code in a word processor and did a find/replace to replace each occurrence of "NUMBER" with "100". So your compiler has no idea about the existence of NUMBER
. Only the pre-compilation preprocessor knows what NUMBER
means.
因此,如果您尝试使用 NUMBER
的地址,则编译器会认为您尝试使用整数文字常量的地址,该地址无效。
So, if you try to take the address of NUMBER
, the compiler will think you are trying to take the address of an integer literal constant, which is not valid.
这篇关于指向#define的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!