什么是&& C中没有左侧时,操作员该怎么办? [英] What does a && operator do when there is no left side in C?

查看:92
本文介绍了什么是&& C中没有左侧时,操作员该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一个用C语言编写的程序,其代码如下:

static void *arr[1]  = {&& varOne,&& varTwo,&& varThree};

varOne: printf("One") ;
varTwo: printf("Two") ;
varThree: printf("Three") ;

我对&&的功能感到困惑,因为它的左边没有任何内容.默认情况下它是否评估为null?还是这是特例?

添加了更多信息,以使问题/代码对我的问题更加清晰. 谢谢大家的帮助.这是gcc特定扩展名的情况.

解决方案

这是gcc特定的扩展名,可以用于标签名称的一元&&运算符,其地址为void*值. /p>

作为扩展名的一部分,允许goto *ptr;,其中ptr是类型void*的表达式.

它已在gcc手册的此处中记录. /p>

您可以获得当前函数中定义的标签的地址(或 一元运算符&&包含一个包含函数).该值具有 键入void *.该值是一个常数,可以在以下任何地方使用: 该类型的常数有效.例如:

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

要使用这些值,您需要能够跳到一个.这个完成了 与计算的goto语句goto *exp;一起使用.例如,

goto *ptr;

允许使用任何类型为void *的表达式.

正如zwol在评论中指出的那样,gcc使用&&而不是更明显的&,因为标签和具有相同名称的对象可以同时可见,如果&表示&foo可能不明确标签地址".标签名称占据它们自己的名称空间(不是C ++含义),并且只能在特定的上下文中显示:由 labeled-statement 定义,作为goto语句的目标,或者对于gcc ,作为一元&&的操作数.

I saw a program in C that had code like the following:

static void *arr[1]  = {&& varOne,&& varTwo,&& varThree};

varOne: printf("One") ;
varTwo: printf("Two") ;
varThree: printf("Three") ;

I am confused about what the && does because there is nothing to the left of it. Does it evaluate as null by default? Or is this a special case?

Edit: Added some more information to make the question/code more clear for my question. Thank you all for the help. This was a case of the gcc specific extension.

解决方案

It's a gcc-specific extension, a unary && operator that can be applied to a label name, yielding its address as a void* value.

As part of the extension, goto *ptr; is allowed where ptr is an expression of type void*.

It's documented here in the gcc manual.

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 statement, goto *exp;. For example,

goto *ptr;

Any expression of type void * is allowed.

As zwol points out in a comment, gcc uses && rather than the more obvious & because a label and an object with the same name can be visible simultaneously, making &foo potentially ambiguous if & means "address of label". Label names occupy their own namespace (not in the C++ sense), and can appear only in specific contexts: defined by a labeled-statement, as the target of a goto statement, or, for gcc, as the operand of unary &&.

这篇关于什么是&& C中没有左侧时,操作员该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆