作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对下面的表达式很好奇:
int ints[] = { 1, 2, 3 };
即使在带有 clang 的 c89 环境中,这似乎也可以正常编译。有关于这个的文档吗?在搜索它时,我似乎无法弄清楚要使用的正确术语(而且我不想再次阅读整个 c89 规范)。
这是扩展吗?编译器是否只是推断数组的大小?
编辑:我只记得你们喜欢实际编译的代码块,所以这里是:
/* clang tst.c -o tst -Wall -Wextra -Werror -std=c89 */
int main(int argc, const char *argv[]) {
int ints[] = { 1, 2, 3 };
(void)(ints); (void)(argc); (void)(argv);
return 0;
}
最佳答案
它是标准 C 的一部分,因为 C89 :
§3.5.7 Initialization
If an array of unknown size is initialized, its size is determined by the number of initializers provided for its members. At the end of its initializer list, the array no longer has incomplete type.
其实还有一个几乎完全一样的例子:
Example:
The declaration
int x[] = { 1, 3, 5 };
defines and initializes
x
as a one-dimensional array object that has three members, as no size was specified and there are three initializers.
关于c - 初始化没有大小的数组字面量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30016477/
1.字面常量 (1)字面意思是啥就是啥,看其表示就可以知道其值和类型。 (2)有值无名,一用来初始化变量,与一种字符相关联。 #include <stdio.h>int main()
我是一名优秀的程序员,十分优秀!