Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments to C program question [duplicate]

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 :-)

like image 684
rubixibuc Avatar asked Aug 04 '26 08:08

rubixibuc


2 Answers

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.

like image 126
Armen Tsirunyan Avatar answered Aug 07 '26 01:08

Armen Tsirunyan


**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;
}
like image 20
DaveR Avatar answered Aug 07 '26 01:08

DaveR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!