C中函数参数中的固定数组或指针之间的区别? [英] Difference between fixed array or pointer in a function parameter in C?
问题描述
之间是否有区别:
void draw_line(float p0[2], float p1[2], float color[4]);
,并且:
void draw_line(float *p0, float *p1, float *color);
在C中?
推荐答案
- 列表项
在C和C ++中,函数声明之间没有区别。
There is no difference between the function declarations neither in C nor in C++.
具有数组类型的函数参数由编译器隐式调整为指向数组元素类型的指针。
A function parameter having an array type is implicitly adjusted by the compiler to pointer to the array element type.
根据C标准(6.7.6.3函数声明符(包括原型))
From the C Standard (6.7.6.3 Function declarators (including prototypes))
7参数声明为类型数组应调整为
为类型的合格指针 ,其中类型限定符
是在数组类型派生的[和]中指定的类型限定符...
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...
因此,这些声明
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.
C和C ++的区别在于,在C中,您可以在方括号中指定限定符,并在表达式中使用关键字static来指定参数将提供访问的元素数量。
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.
- ...如果关键字static也出现在数组$ b $的[和]中b类型派生,然后对于每次调用该函数,对应的实际参数
的值应提供对数组的第一个
元素的访问,该元素的数量至少应与$ b $指定的数量相同b大小表达式。
在C和C ++中,用作此类参数的参数的数组也都隐式地存在。转换为指向其第一个元素的指针。
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中函数参数中的固定数组或指针之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!