gpt4 book ai didi

c - 这段代码是否有数组大小的有效声明?

转载 作者:行者123 更新时间:2023-11-30 18:28:15 24 4
gpt4 key购买 nike

我正在学习如何用 C 语言对数组进行编程。我对以下有关数组大小的代码有疑问。在下面的代码中,它是数组大小的有效声明吗?请解释一下它是有效还是无效。

#include<stdio.h>
#define SIZE 10

int main(void) {
int size=12;
float salary[size];
salary[0]=890.54;
printf("%f",salary[0]);
return 0;
}

最佳答案

为了支持另一个答案,如果支持可变长度数组 (VLA),那么是的 — 问题中的声明有效并定义了 VLA。 VLA 在 C99 中首次亮相,然后在 C11 中变为可选。不支持 VLA 的符合 C11 编译器定义 __STDC_NO_VLA__

来自§6.7.6.2¶4

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations or type names with function prototype scope;143) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type. (Variable length arrays are a conditional feature that implementations need not support; see 6.10.8.3.)

来自§6.10.8.3¶1

__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types

关于c - 这段代码是否有数组大小的有效声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48606251/

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