Re: argv/pointer problems
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 06/16/04
- Next message: John Harrison: "Re: Creating own IO streams"
- Previous message: Razvan: "Re: Which scope is searcheg first ?"
- In reply to: Robin Sanderson: "Re: argv/pointer problems"
- Next in thread: John Harrison: "Re: argv/pointer problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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[3]" 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)?
int main()
{
int i;
int argc=3;
char * args[3];
args[0] = "program_name";
args[1] = "argument_1";
args[2] = "argument_2";
char * (*argv) = args;
// declares argv as a pointer to the array args
for(i=0;i<argc;i++)
{
cout << "i="<<i<<"; *argv = " << *argv << endl;
argv++;
}
return 0;
}
- Next message: John Harrison: "Re: Creating own IO streams"
- Previous message: Razvan: "Re: Which scope is searcheg first ?"
- In reply to: Robin Sanderson: "Re: argv/pointer problems"
- Next in thread: John Harrison: "Re: argv/pointer problems"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|