Possible Duplicate:
Should I use char** argv or char* argv[] in C?
What is the difference between using char **argv
and char *argv[]
for the second parameter to a c program. Does it affect the way the strings are passed? Thanks :-)
Possible Duplicate:
Should I use char** argv or char* argv[] in C?
What is the difference between using char **argv
and char *argv[]
for the second parameter to a c program. Does it affect the way the strings are passed? Thanks :-)
There is absolutely no difference. At all.
in C
void f(int*);
and
void f(int[]);
and
void f(int[42]);
are three completely identical declarations.
**argv
and *argv[]
as a function argument are technically identical - as C will convert any array into a pointer in a function argument.
Note that this can catch you out, for example this is totally valid and won't raise any warnings (or errors), even though a beginner might think that the array passed is invalid:
void func(int array[2])
{
array[1] = 1; // buffer overrun: will write past the end of a[].
}
int main()
{
int a[1];
func(a);
return 0;
}
In the context of a function parameter declaration, T a[]
and T *a
are synonymous. In both cases, a
is a pointer to T
.
When an array expression appears in a context other than as an operand to the sizeof
or unary &
operators (such as a parameter in a function call), the type of the expression is implicitly converted from "N-element array of T
" to "pointer to T
", and the value of the expression is the address of the first element in the array. Thus, a function can never receive an array value; it can only receive a pointer to the first element.
dmr decided to use the T a[]
syntax to make the intent clear (a
refers to the first element of an array of T
as opposed to a pointer to a single instance of T
), but I think it's caused more heartburn that it was worth.