Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I declare the expected size of an array passed as function argument?

Tags:

c

I think both is valid C syntax, but which is better?

A)

void func(int a[]);  // Function prototype
void func(int a[]) { /* ... */ }  // Function definition

or

B)

#define ARRAY_SIZE 5
void func(int a[ARRAY_SIZE]);  // Function prototype
void func(int a[ARRAY_SIZE]) { /* ... */ }  // Function definition
like image 254
Sebastian Roll Avatar asked Mar 03 '10 20:03

Sebastian Roll


2 Answers

The main reason to add an array size is for documentation purposes. Also, with C99, you can add qualifiers within the square brackets which will be used to modify the pointer declaration to which the array declaration will be converted if it occurs within a parameter list.

See C99 spec, section 6.7.5.3, §7:

A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. If the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression.

and §21:

EXAMPLE 5 The following are all compatible function prototype declarators.

double maximum(int n, int m, double a[n][m]);
double maximum(int n, int m, double a[*][*]);
double maximum(int n, int m, double a[ ][*]);
double maximum(int n, int m, double a[ ][m]);

as are:

void f(double (* restrict a)[5]);
void f(double a[restrict][5]);
void f(double a[restrict 3][5]);
void f(double a[restrict static 3][5]);

(Note that the last declaration also specifies that the argument corresponding to a in any call to f must be a non-null pointer to the first of at least three arrays of 5 doubles, which the others do not.)

like image 186
Christoph Avatar answered Sep 19 '22 18:09

Christoph


Actually, it doesn't make a difference, Because the size is lost anyway.

like image 40
Khaled Alshaya Avatar answered Sep 19 '22 18:09

Khaled Alshaya