gpt4 book ai didi

c - 哪个 C99 编译器(Clang 与 GCC)更接近 const 结构字段的标准?

转载 作者:太空狗 更新时间:2023-10-29 15:41:37 25 4
gpt4 key购买 nike

我有这样的代码:

$ cat test.c 
#include <stdio.h>
typedef struct
{
const int x;
} SX;

static SX mksx(void)
{
return (SX) { .x = 10 };
}

void fn(void)
{
SX sx;
while((sx = mksx()).x != 20)
{
printf("stupid code!");
}
}

关于其正确性的 2 个意见:

$ for i in gcc clang; do echo "$i SAYS:"; $i -c -std=c99 -pedantic -Werror test.c; done
gcc SAYS:
test.c: In function ‘fn’:
test.c:15:2: error: assignment of read-only variable ‘sx’
while((sx = mksx()).x != 20)
^
clang SAYS:

哪个编译器是正确的?

最佳答案

C99 标准在 6.5.16:2 中说:

An assignment operator shall have a modifiable lvalue as its left operand.

在 6.3.2.1:1 中:

A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const-qualified type, and if it is a structure or union, does not have any member (including, recursively, any member or element of all contained aggregates or unions) with a const-qualified type.

因此 GCC 发出警告是正确的。

此外,条款 6.5.16:2 位于 C99 标准的“约束”部分,因此符合标准的编译器需要为违反该条款的程序发出诊断。它仍然是未定义的行为:在发出诊断后,编译器仍然可以做它想做的事。但必须有一个消息。因此,Clang 在这里以不符合规范的方式行事。

关于c - 哪个 C99 编译器(Clang 与 GCC)更接近 const 结构字段的标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42224637/

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