Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between these two declarations? [duplicate]

Tags:

c

Possible Duplicate:
Difference between passing array and array pointer into function in C

I have been wondering this for a while, is there any difference between these two?

void f1(char *c);
void f2(char c[]);

A common example is this:

int main(int argc, char **argv);
int main(int argc, char *argv[]);

Are there any reasons to prefer one to the other, apart from artistic ones?

like image 512
orlp Avatar asked Jan 17 '23 21:01

orlp


2 Answers

There is no difference.

From the horse mouth:

(C99, 6.7.5.3p7) "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."

On the reasons to prefer one form over the other, it depends on the people. Here is what H&S says (on switching to T *array from T arr[]):

(H&S, 5.4.3 Array Bounds) "That would more accurately reflect the implementation but less clearly indicate the intent."

like image 153
ouah Avatar answered Jan 30 '23 02:01

ouah


No. In a function declaration/definition, the use of an array as an argument is syntactic sugar only. It's still passed as a pointer, and sizeof(theArgument) will still give you sizeof(TheType *) rather than sizeof(TheType) * sizeof(numElements).

like image 39
Jonathan Grynspan Avatar answered Jan 30 '23 00:01

Jonathan Grynspan