Re: argv/pointer problems
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: Wed, 16 Jun 2004 11:41:42 +0200
Robin Sanderson wrote:
> I have just noticed that I posted the wrong version of Programm 3 -
> the "** argv" should have been "* argv", and this is corrected below
> - although this does not alter whether or not it compiles.
> This may be a(nother) daft question, but what is it that makes the
> difference between the use of "char * argv" in Program 2 and "char
> * argv" in Program 3?
In program 3, it's a parameter, while in program 2, it's a local
variable. You cannot pass arrays as parameters to functions, and
therefore, a "convenience" was added to C (and inherited to C++). If
you write a parameter in a form that looks like an array, it will
actually be a pointer. That's btw the reason why you can leave out the
size. When defining an array, you _always_ need a size for it.
> Is it possible to declare argv as a pointer in Program 2 (so it can be
> used in an identical way as in the other programs)?
char * args;
args = "program_name";
args = "argument_1";
args = "argument_2";
char * (*argv) = args;
// declares argv as a pointer to the array args
cout << "i="<<i<<"; *argv = " << *argv << endl;