[英]Is it possible to get the address of a macro?
#define N 100
&N
not possible. &N
不可能。 How can I know the address of N? 我怎么知道N的地址? It must have some address.
它必须有一些地址。
宏没有任何地址,这就是为什么它们被称为常量值。
A macro is simple text substitution. 宏是简单的文本替换。 If you write code like
如果你写代码
int* ptr = &N;
it will then be pre-processed into 然后它将被预处理成
int* ptr = &100;
100
is an integer literal and all integer literals are constant rvalues, meaning you can't assign a value to it, nor can you take its address. 100
是整数字面值 ,所有整数字面值都是常量rvalues,这意味着您无法为其赋值,也无法获取其地址。
The literal 100
is of course stored in memory though - you can't allocate numbers in thin air - but it will most likely be stored as part of the executed program code. 文字
100
当然存储在内存中 - 你无法在空中分配数字 - 但它很可能存储为已执行程序代码的一部分。 In the binary, you'll have some machine code instruction looking like "store 100 in the memory location of the pointer variable" and the 100
is stored in that very machine code instruction. 在二进制文件中,您将有一些机器代码指令看起来像“在指针变量的内存位置存储100”,并且
100
存储在该机器代码指令中。 And a part of a machine code instruction isn't addressable. 并且机器代码指令的一部分是不可寻址的。
It is simple text substitution. 这是简单的文本替换。 The compiler's pre-processor replaces all occurrences of
N
with 100
, but, it depends on what N
is. 编译器的预处理器将所有出现的
N
替换为100
,但是,它取决于N
是什么。 In your example taking the address of a constant won't compile, but these two other examples do work. 在您的示例中,获取常量的地址将无法编译,但这两个其他示例确实有效。
#include <stdio.h>
#define N 100
#define M x
#define L "Hallo world!"
int main()
{
int x = 42;
//printf ("Address of 'N' is %p\n", (void*)&N); // error C2101: '&' on constant
printf ("Address of 'M' is %p\n", (void*)&M);
printf ("Address of 'L' is %p\n", (void*)&L);
return 0;
}
Program output: 节目输出:
Address of 'M' is 0018FF3C
Address of 'L' is 0040C018
MORE explanation. 更多解释。
With #define N 100
you can't get the address of N
because a numerical constant like that does not have a single memory location. 使用
#define N 100
您无法获得N
的地址,因为像这样的数字常量没有单个内存位置。 100
might be assigned to the value of a variable, or indeed the optimising compiler might load 100
directly into a processor register. 可以将
100
分配给变量的值,或者实际上优化编译器可以将100
直接加载到处理器寄存器中。
In the case of #define M x
that's a simple substitution so that M
can be used exactly as x
can. 在
#define M x
的情况下,这是一个简单的替换,因此M
可以完全像x
一样使用。 There is no functional difference between &x
and &M
because the two statements are identical after the preprocessor has made the substitution. &x
和&M
之间没有功能差异,因为在预处理器进行替换后,这两个语句是相同的。
In the case of #define L "Hallo world!"
在
#define L "Hallo world!"
的情况下#define L "Hallo world!"
we have a string literal, which the compiler does place in memory. 我们有一个字符串文字,编译器确实放在内存中。 Asking for
&L
is the same as asking for &"Hallo world!"
要求
&L
与要求&"Hallo world!"
and that is what you get. 这就是你得到的。
N
is not a variable, it never has any address. N
不是变量,它从来没有任何地址。 It's just a value to get pasted in when you use patterns like int val=N;
当你使用像
int val=N;
这样的模式时,它只是一个被粘贴的值int val=N;
. 。 Then you can get the address of
val
using &
. 然后你可以使用
&
获取val
的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.