[英]Difference between fixed array or pointer in a function parameter in C?
Is there a difference between:是否有区别:
void draw_line(float p0[2], float p1[2], float color[4]);
and this:和这个:
void draw_line(float *p0, float *p1, float *color);
in C?在 C?
There is no difference between the function declarations neither in C nor in C++. C 和 C++ 中的函数声明之间没有区别。
A function parameter having an array type is implicitly adjusted by the compiler to pointer to the array element type.具有数组类型的函数参数被编译器隐式调整为指向数组元素类型的指针。
From the C Standard (6.7.6.3 Function declarators (including prototypes))来自 C 标准(6.7.6.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... 7 将参数声明为“类型数组”应调整为“类型限定指针”,其中类型限定符(如果有)是在数组类型派生的 [ 和 ] 中指定的那些。 .
Thus these declarations因此这些声明
void draw_line(float p0[2], float p1[2], float color[4]);
void draw_line(float *p0, float *p1, float *color);
declare the same one function and the both can be present in a program though the compiler can issue a message that there are redundant declarations.声明相同的一个函数并且两者都可以出现在程序中,尽管编译器可以发出一条消息,表明存在冗余声明。
The difference between C and C++ is that in C you can specify in brackets qualifiers and an expression with the keyword static that specifies the number of elements the arguments shall provide the access to. C 和 C++ 之间的区别在于,在 C 中,您可以在括号中指定限定符和带有关键字 static 的表达式,该关键字指定参数应提供访问的元素数量。
- ...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. ...如果关键字 static 也出现在数组类型派生的 [ 和 ] 中,那么对于函数的每次调用,相应实参的值应提供对数组第一个元素的访问由大小表达式指定的元素。
In the both C and C++ an array used as an argument of such a parameter is also implicitly converted to pointer to its first element.在 C 和 C++ 中,用作此类参数的参数的数组也隐式转换为指向其第一个元素的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.