C/C++标准中定义的struct with flexible array memeber的用法在哪里?
Where is the usage of struct with flexible array memeber defined in the C/C++ standards?
如果我有这样的代码
struct s { int x; char b[]; };
int main() {
struct s s[10];
}
然后我用“gcc -O2 -W -Wall -pedantic”编译然后我得到:
<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
4 | struct s s[10];
| ^
gcc 是完全正确的。具有灵活数组成员的结构不能那样工作。
C/C++ 标准中的什么地方定义了这个?
在 C 中,它是 §6.7.2.1,第 3 段:
A structure or union shall not contain a member with incomplete or function type,… except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.
与@phuclv points out in a 一样,C++没有这个特性。
如果我有这样的代码
struct s { int x; char b[]; };
int main() {
struct s s[10];
}
然后我用“gcc -O2 -W -Wall -pedantic”编译然后我得到:
<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
4 | struct s s[10];
| ^
gcc 是完全正确的。具有灵活数组成员的结构不能那样工作。
C/C++ 标准中的什么地方定义了这个?
在 C 中,它是 §6.7.2.1,第 3 段:
A structure or union shall not contain a member with incomplete or function type,… except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.
与@phuclv points out in a