5

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
  • I once thought about the same thing, and ended up wrapping the array inside a struct to declare a special type to pass to the function, since the array-length would never change within the program (it was a 3D vector). – S.C. Madsen Mar 03 '10 at 21:39

3 Answers3

7

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

Christoph
  • 164,997
  • 36
  • 182
  • 240
4

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

Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
1

Generally for most function that are expecting arrays you will see a pointer passed along with the size of the array. In C you allways have to keep track of how big your arrays are.

Ex

void func(int *ary,int szarry){...}
rerun
  • 25,014
  • 6
  • 48
  • 78