# 注意 是 C语言代码,不是C++
void func(void)
{
const int buf_len = 64;
char buffer[buf_len] = { 0 };
}
$$cat -n fun.c
1 void func(void)
2 {
3 const int buf_len = 64;
4 char buffer[buf_len] = { 0 };
5 }
# gcc 编译报错 :(
$gcc -c fun.c
fun.c: In function ‘func’:
fun.c:4:5: error: variable-sized object may not be initialized
4 | char buffer[buf_len] = { 0 };
| ^~~~
# gcc 版本如下
$gcc --version
gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# clang 11.x 编译完全没有错误:)
clang -c fun.c
# clang 版本如下
$clang --version
Debian clang version 11.0.1-2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
# 因为项目用的是GCC,所以放弃这个写法 :)
标签:gcc,void,厂家,len,C语言,编译器,clang,fun,buf
From: https://www.cnblogs.com/wangshaodong/p/17179114.html