Difference in methods to pass array to function in C

Is there a difference between defining:

void example(int usuario[]);

or

void example(int usuario[SIZE]);

Upvotes: 0

Views: 59

Answers (1)

kabanus
kabanus

Reputation: 25895

In c, the array name when passed decays to a pointer. Using a sizeof in both your examples on usario will return the size of an int pointer, regardless of SIZE. So both examples are identical, and are the same as

void example (int *usario)

The [] syntax in this context is purely syntactic sugar. Functionally, a pointer will be what you are actually working with - the [] or [SIZE] are only useful for a future programmer to see you expect an array with length SIZE. This will not be enforced by the compiler at all.

On a modern compiler, you will get a warning if you do sizeof(usario) in the function - you can try it with both your examples.

Upvotes: 3

Related Questions