不要括号确定数组的大小时,有所作为? [英] Do parentheses make a difference when determining the size of an array?

查看:85
本文介绍了不要括号确定数组的大小时,有所作为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的程序打印了相同的号码在GCC 4.8.2:

The following program prints the same number twice on gcc 4.8.2:

#include <stdio.h>

int main()
{
    char a[13];
    printf("sizeof a  is %zu\n", sizeof a );
    printf("sizeof(a) is %zu\n", sizeof(a));
}

据<一个href=\"https://www.reddit.com/r/programming/comments/37tnra/humans_should_think_of_sizeof_as_a_function_says/crq05zc\">this reddit的帖子,海湾合作委员会是在这方面不符合标准的,因为一个括号前pression不例外的,当阵列到指针腐烂不会发生在名单上。

According to this reddit post, gcc is not standard-conformant in this respect, because a parenthesized expression is not on the list of exceptions for when array-to-pointer decay does not happen.

这家伙是正确的?下面是相关标准的报价:

Is this guy correct? Here is the relevant standard quote:

除非它是的sizeof 的运算数或一元&安培; 运营商,或者是一个字符字符串用来初始化字符类型的数组,或者是用于初始化元素类型与 wchar_t的兼容的,即具有类型的阵列左值数组中的宽字符串文字类型'转换到具有键入前pression'指向类型为指向数组对象的初始成员,是不是左值。

Except when it is the operand of the sizeof operator or the unary & operator, or is a character string literal used to initialize an array of character type, or is a wide string literal used to initialize an array with element type compatible with wchar_t, an lvalue that has type 'array of type' is converted to an expression that has type 'pointer to type' that points to the initial member of the array object and is not an lvalue.

只是要清楚,他认为,(一)应触发数组到指针的衰减,因为括号没有在上面(<$ C $名单所涉及C>的sizeof 符,一元&放大器; 运营商,字符串作为初始化)

Just to be clear, he argues that (a) should trigger array-to-pointer decay, because parentheses are not covered in the list above (sizeof operator, unary & operator, string literal as initializer).

推荐答案

无论是看似多余的括号影响程序的语义是在C标准还没有得到充分解决了长期存在的问题。

Whether seemingly redundant parentheses affect the semantics of a program is a long-standing issue in the C standard that still hasn't been adequately resolved.

人们普遍声称,((无效*)0)在技术上是不是一个空指针常数,因为没有规定说一个parenthesised空指针常量是一个空指针常量。

It is commonly claimed that ((void*)0) is technically not a null pointer constant, because there is no rule that says a parenthesised null pointer constant is a null pointer constant.

一些编译器问题为个char [] =(ABC)的错误; ,因为当一个字符数组可以从一个字符串初始化文字,该规则没有按T盖parenthesised字符串。

Some compilers issue an error for char s[] = ("abc");, because while a character array can be initialised from a string literal, that rule doesn't cover parenthesised string literals.

有很多类似的例子。你已经找到了其中的一个。

There are many similar examples. You've found one of them.

从我所知道的,concensus基本上是规则的的是什么C ++做什么,但C从不正式通过。 C ++使得parenthesised前pression功能等效非parenthesised前pression,与几个明确态异常。这将包括所有这些问题的一次。

From what I can tell, the concensus is basically that the rule should be what C++ does, but what C never formally adopted. C++ makes a parenthesised expression functionally equivalent to the non-parenthesised expression, with a few explicitly-stated exceptions. This would cover all those issues at once.

所以从技术上来说,这个家伙可能被认为是正确的,但它是标准的过于严格的跨pretation,没有人真正遵循,因为它是人所共知的标准仅仅是故障在这里。

So technically, the guy could be considered correct, but it's an overly strict interpretation of the standard that nobody really follows, since it's common knowledge that the standard is simply faulty here.

这篇关于不要括号确定数组的大小时,有所作为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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