user3262424
user3262424

Reputation: 7479

C Question -- Using [] Instead of *?

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

float DoSomething( const float arr[] );

Vs.

float DoSomething( const float* arr );

Is one preferable than the other?

Upvotes: 0

Views: 233

Answers (3)

Anders Abel
Anders Abel

Reputation: 69260

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.

Upvotes: 7

Aliostad
Aliostad

Reputation: 81660

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

Upvotes: 0

Jason
Jason

Reputation: 32510

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.

Upvotes: 1

Related Questions