While browsing some source code I came across a function like this:
在浏览一些源代码时,我遇到了这样一个函数:
void someFunction(char someArray[static 100])
{
// do something cool here
}
With some experimentation it appears other qualifiers may appear there too:
通过一些实验,似乎还会出现其他的限定词:
void someFunction(char someArray[const])
{
// do something cool here
}
It appears that qualifiers are only allowed inside the [
]
when the array is declared as a parameter of a function. What do these do? Why is it different for function parameters?
当数组被声明为函数的参数时,限定符只允许在[]内。这些做什么?为什么函数参数不同?
1 个解决方案
#1
105
The first declaration tells the compiler that someArray
is at least 100 elements long. This can be used for optimizations. For example it also means that someArray
is never NULL.
第一个声明告诉编译器someArray至少有100个元素长。这可以用于优化。例如,它也意味着someArray从不为空。
Note that the C Standard does not require the compiler to diagnose when a call to the function does not meet these requirements (i.e. it is silent undefined behaviour).
注意,当对函数的调用不满足这些要求时,C标准不要求编译器进行诊断(例如,它是静默的未定义行为)。
The second declaration simply declares someArray
(not someArray
's elements!) as const, i.e. you can not write someArray=someOtherArray
. It is the same as if the parameter were char * const someArray
.
第二个声明只是声明someArray(不是someArray的元素!)为const,即不能写入someArray=其他数组。这与参数为char * const someArray是一样的。
This syntax is only usable within the innermost []
of an array declarator in a function parameter list, it would not make sense in other contexts.
此语法仅在函数参数列表中的数组声明符的最内层[]中可用,在其他上下文中没有意义。
The Standard text, which covers both of the above cases, is in C11 6.7.6.3/7 (was 6.7.5.3/7 in C99):
上述两种情况的标准文本在C11 6.7.6.3/7 (C99中为6.7.5.3/7):
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the
[
and]
of the array type derivation. If the keyword static also appears within the[
and]
of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.将参数声明为“类型数组”,应调整为“合格指针类型”,其中类型限定符(如果有的话)是在数组类型派生的[和]中指定的类型。如果关键字静态也出现在[和]数组类型的推导,然后为每个调用函数,相应的实际参数的值应当提供一个数组的第一个元素至少尽可能多的元素大小指定的表达式。
#1
105
The first declaration tells the compiler that someArray
is at least 100 elements long. This can be used for optimizations. For example it also means that someArray
is never NULL.
第一个声明告诉编译器someArray至少有100个元素长。这可以用于优化。例如,它也意味着someArray从不为空。
Note that the C Standard does not require the compiler to diagnose when a call to the function does not meet these requirements (i.e. it is silent undefined behaviour).
注意,当对函数的调用不满足这些要求时,C标准不要求编译器进行诊断(例如,它是静默的未定义行为)。
The second declaration simply declares someArray
(not someArray
's elements!) as const, i.e. you can not write someArray=someOtherArray
. It is the same as if the parameter were char * const someArray
.
第二个声明只是声明someArray(不是someArray的元素!)为const,即不能写入someArray=其他数组。这与参数为char * const someArray是一样的。
This syntax is only usable within the innermost []
of an array declarator in a function parameter list, it would not make sense in other contexts.
此语法仅在函数参数列表中的数组声明符的最内层[]中可用,在其他上下文中没有意义。
The Standard text, which covers both of the above cases, is in C11 6.7.6.3/7 (was 6.7.5.3/7 in C99):
上述两种情况的标准文本在C11 6.7.6.3/7 (C99中为6.7.5.3/7):
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the
[
and]
of the array type derivation. If the keyword static also appears within the[
and]
of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.将参数声明为“类型数组”,应调整为“合格指针类型”,其中类型限定符(如果有的话)是在数组类型派生的[和]中指定的类型。如果关键字静态也出现在[和]数组类型的推导,然后为每个调用函数,相应的实际参数的值应当提供一个数组的第一个元素至少尽可能多的元素大小指定的表达式。