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 中?

  1. 列表项

C 和 C++ 中的函数声明没有区别。

具有数组类型的函数参数被编译器隐式调整为指向数组元素类型的指针。

来自 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...

因此这些声明

void draw_line(float p0[2], float p1[2], float color[4]);

void draw_line(float *p0, float *p1, float *color);

声明同一个函数,两者都可以出现在程序中,尽管编译器会发出一条消息,指出存在冗余声明。

C 和 C++ 之间的区别在于,在 C 中,您可以在括号中指定限定符和带有关键字 static 的表达式,该关键字指定参数应提供的元素数。

  1. ...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.

在 C 和 C++ 中,用作此类参数的参数的数组也被隐式转换为指向其第一个元素的指针。