开发者

C Question -- Using [] Instead of *?

开发者 https://www.devze.com 2023-03-22 09:41 出处:网络
In C, is there a difference between the following declarations: float DoSomething( const float arr[] );

In C, is there a difference between the following declarations:

float DoSomething( const float arr[] );

Vs.

float D开发者_StackOverflow社区oSomething( const float* arr );

Is one preferable than the other?


No, there isn't any difference. An array is decayed into a pointer whenever it is passed as an argument to a function.

I think that using the array syntax is a bit clearer, it gives a hint that it is an array and not just a pointer to one object.

There is a quite detailed description of pointers and arrays at http://www.c-faq.com/aryptr/index.html.


There isn't a difference because of implicit pointer conversions for array-types when passed as arguments to functions.

Additionally, when you use the value of arr in your first version of DoSomething that uses array-syntax, the value of arr will again be implicitly converted to a pointer when used either by itself on the right-hand side of the assignment statement, or when used with the [] array-access operator.


Every array can be represented as pointer but not every pointer can be represented as array.

0

精彩评论

暂无评论...
验证码 换一张
取 消