gpt4 book ai didi

c - 为什么 gcc 使用 && 一元运算符而不是给出标签的直接地址?

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:32 25 4
gpt4 key购买 nike

我有这样的疑问,为什么 gcc 使用 && 来访问标签,为什么它不直接提供对与标签关联的位置值的访问。就像下面的代码:

void main()
{
static void* loc = &&locA;
printf("locA : %p\n", locA); //will give error
printf("loc : %p\n",loc); //will not give error
//statments X
locA :
//statements Y

}

最佳答案

这是文档的 gcc 扩展 Extensions to the C Language Family of the gcc documentationLabels as Values 部分其中说:

You can get the address of a label defined in the current function (or a containing function) with the unary operator ‘&&’. The value has type void *. This value is a constant and can be used wherever a constant of that type is valid. For example:

 void *ptr;
/* ... */
ptr = &&foo;

To use these values, you need to be able to jump to one. This is done with the computed goto statement1, goto *exp;. For example,

 goto *ptr;

One way of using these constants is in initializing a static array that serves as a jump table:

 static void *array[] = { &&foo, &&bar, &&hack };

Then you can select a label with indexing, like this:

 goto *array[i];

关于c - 为什么 gcc 使用 && 一元运算符而不是给出标签的直接地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com