haccks
haccks

Reputation: 106102

Understanding Function prototypes

Are the function prototypes

int sum_array(int array[], int arr_length);  

and

int sum_array(int [], int arr_length);  

similar?
If yes then, what does int [] mean?
Can i define above prototypes by swapping their positions,i.e

 int sum_array( int arr_length, int array[]); 

?
NOTE:I have no idea about pointers(sorry for that).

Upvotes: 0

Views: 107

Answers (2)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14530

Both prototypes are the same. The first one just give a name to the first parameter :

int sum_array(int array[], int arr_length);

or

int sum_array(int [], int arr_length); 

are the same. Naming the parameters in prototype is just for information purpose only.

In the same way, you can do :

int sum_array(int [], int);

After that the implementation will look like :

int sum_array(int array[], int arr_length)
{ ... }

But you can't swap the parameters, that is not the same thing. If you swap the parameters, the implementation and the calls to this function must have the parameters swapped too.

Upvotes: 4

Taylor Brandstetter
Taylor Brandstetter

Reputation: 3623

The C standard states that you MAY declare identifiers for the parameters in the function prototype, but you don't have to.

The identifiers ... are declared for descriptive purposes only and go out of scope at the end of the declaration

So to answer your first question, there is essentially no difference between the first two prototypes. And int [] means "an array of integers", in a similar way that int array[] means "an array of integers identified as array".

The third prototype also works, but the variables will be pushed on the stack the opposite order. You can do this as long as the prototype and definition use the same ordering.

Upvotes: 2

Related Questions