0

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).

haccks
  • 104,019
  • 25
  • 176
  • 264
  • In a prototype, `int a_very_long_name[]`, `int a_long_name[]`, `int a_name[]`, `int a[]`, and `int []` all mean the same thing. – Jim Balter Jun 28 '13 at 00:30
  • @JimBalter; But i think in last one i.e `int []`, has no variable name . – haccks Jun 28 '13 at 00:38
  • Yes ... what's your point? The variable name isn't used in a prototype (in most cases, such as this one; there are exceptions), it's just for clarity / documentation. You've already read the answers and accepted one, so you should know this by now. – Jim Balter Jun 28 '13 at 00:42
  • My point is clear......i was confused with `int []`. I thought that how it is representing an array without a variable name. – haccks Jun 28 '13 at 00:46
  • Not much of what you write is clear ... that last sentence certainly isn't. – Jim Balter Jun 28 '13 at 03:04
  • I mean to say in prototype `int sum(int , int );`, `int` inside the parenthesis indicates that it is expecting `int` type arguments.Similarly in above cases `int []` is also representing **type** of argument(first) which must be of **integer array type**.When i encountered `int []` first time i was not aware that it is representing a type. – haccks Jun 28 '13 at 10:04

2 Answers2

4

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.

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • what is the meanig of `int []`?? – haccks Jun 27 '13 at 20:26
  • 1
    It just says to the compiler that the parameter will be an array of `int`. It is the same as `int array[]`, but here you give a name to the parameter for descriptive purpose. I prefer naming the parameters in my prototype, it make the reading of header files easier for me. – Pierre Fourgeaud Jun 27 '13 at 20:28
2

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.

Taylor Brandstetter
  • 3,523
  • 15
  • 24