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?
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."
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With