help difference between char buf[5] and char * arg[5]

what is the difference between those 2 functions

int foo(char *ch[5]){}

int foo(char ch[5]){}

why and when i can specify char array size in the function parameter list?
should i use char * instead??
or use int foo(char ch[],int n_elemtens){}

help!
Last edited on
http://www.unixwiz.net/techtips/reading-cdecl.html

char *ch[5] is an array of 5 pointer to char.
char ch[5] is an array of 5 char.

You are perhaps thinking about the equivalence between pointers and arrays as function parameters.

1
2
3
4
int foo(char ch[5]); // same as
int foo(char *ch); // same as
int foo(char ch[]); // same as
int foo(char ch[291]);


A feature of the C language that C++ inherited is that arrays decay to pointers.
In other words, whenever you use the name of an array, it becomes a pointer to the first element.

So all the "array" ch parameters above are just syntactic sugar for char *ch.

Things change a bit when you use multidimensional arrays.
If you pass a multidimensional array, you must pass the dimensions with it (the first dimension is optional).

1
2
3
4
5
int matrix[10][20];

void func1(int matrix[10][20]); // OK
void func2(int matrix[][20]); // OK
void func3(int matrix[][]); // error! 


Of course, you can still use the equivalence explained at the beginning to make the code messier:

void func4(int (*matrix)[20]); // OK, pointer to array of 20 int
Last edited on
Topic archived. No new replies allowed.