What does “[*]” (star modifier) mean in C? [duplicate]

[*]

What is its purpose, why was it added?

Purpose is seen when a variable length two dimentional array is used as a function parameter. The function

int foo(int n, int m, int a[n][m])  {...}   

can be prototyped as any of the following

int foo(int , int, int [][*]);
int foo(int , int, int a[*][*]);
int foo(int , int, int (*a)[*]);
int foo(int n, int, int a[n][*]);
int foo(int , int m, int a[*][m]);
int foo(int , int m, int (*a)[m]);
int foo(int n, int m, int a[n][m]); 

In case of two dimensional array, when used as function parameter, size of the second dimension can’t be omitted. If the name of first variables in function prototype is omitted then it wouldn’t be possible to specify the length (second dimension) of the array. The * gives the clue that the length of the array will be determined by the second parameter.

What’s the difference with int[]?
What’s the difference with int *?

In case of 1D array, for the function definition

int bar(int n, int a[n]} {...}  

any of the following prototype is valid

int bar (int , int *);
int bar (int , int [*]);
int bar (int , int []);
int bar (int n, int a[]);
int bar (int n, int a[n]);
int bar (int n, int [n]);   

In this case neither * nor n is necessary as compiler will treat both of int [*] and int [n] as int *. So, with one dimensional array you can’t see much difference.


NOTE: When using variable length array as a function parameter, order of parameter is important. Order of parameters for first four prototypes of bar can be switched, but in latter two first parameter must not be the array itself.

int bar (int a[n], int n);  //Wrong. Compiler has not yet seen 'n'.
  

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)