简体   繁体   English

C中函数参数中的固定数组或指针之间的区别?

[英]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?

  1. List item项目清单

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 的表达式,该关键字指定参数应提供访问的元素数量。

  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. ...如果关键字 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.

相关问题 指针参数和数组参数之间有区别吗? - Is there a difference between a pointer parameter and an array parameter? C中将数组和数组指针传递给函数的区别 - Difference between passing array and array pointer into function in C 传递数组,固定大小的数组和数组的基地址之间的区别作为函数参数 - Difference between passing array, fixed-sized array and base address of array as a function parameter C中字符串的数组和指针表示法之间的区别 - difference between array and pointer notation of strings in C 从 c 中的 function 返回数组时,static 数组声明和指针数组声明有什么区别 - What is the difference between static array declaration and pointer array declaration when returning an array from the function in c 指向2D数组的指针作为c中函数的参数 - Pointer to 2D array as parameter in function in c C将int数组指针作为参数传递给函数 - C pass int array pointer as parameter into a function C语言中的pointer [0]和* pointer之间的区别? - Difference between pointer[0] and *pointer in C? 指针指针和指向数组的指针之间的区别? - Difference between pointer to pointer and pointer to array? C和C ++之间不完整数组指针转换规则的差异 - Difference in incomplete array pointer conversion rules between C and C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM