数组声明方括号内的星号在C中意味着什么 [英] What does asterisk inside square bracket of array declaration mean in C
问题描述
我正在编写一个自定义C99解析器.我从这获得了语法链接.这个语法说以下是声明数组的有效语法-
I am writing a custom C99 parser. I got the grammar from this link. This grammar says following is a valid syntax for declaring arrays -
int arr[*];
语法的相关部分如下-
direct-declarator ::=
identifier
"(" declarator ")"
direct-declarator "[" type-qualifier-list? assignment-expression? "]"
direct-declarator "[" "static" type-qualifier-list? assignment-expression "]"
direct-declarator "[" type-qualifier-list "static" assignment-expression "]"
direct-declarator "[" type-qualifier-list? "*" "]"
direct-declarator "(" parameter-type-list ")"
direct-declarator "(" identifier-list? ")"
我尝试使用gcc使用此声明编译代码.它给了我以下警告-
I tried compiling a code with this declaration using gcc. It gave me following warning -
错误:函数原型范围以外的其他地方不允许使用"[*]"
error: ‘[*]’ not allowed in other than function prototype scope
因此,我尝试使用这种语法声明函数原型,并且在编译时没有任何错误或警告.我没有得到的是这种语法在语义上可能意味着什么.有解释的专家吗?
So I tried declaring a function prototype with this type of syntax and it compiled without any error or warning. What I am not getting is what can this syntax possibly mean semantically. Any expert with an explanation?
推荐答案
它是未指定大小的可变长度数组的声明器.另外,以下声明
It's a declarator for a variable length array with unspecified size. Furhtermore, the following declaration
void func(size_t n, char s[n]);
等同于简单地写:
void func(size_t n, char s[*]);
以上内容对于编写标头特别有用,在标头中,通常只声明参数类型
The above is particularly useful for writing headers, where you'd normally declare only the parameter types
void func(size_t, char [*]);
这篇关于数组声明方括号内的星号在C中意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!