Re: argc & argv
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Mon, 23 Feb 2009 19:42:36 -0800
On Mon, 23 Feb 2009 08:17:26 -0800 (PST), goodTweetieBird
<goodTweetieBird@xxxxxxxxxxx> wrote:
I am working with a function that accepts arguments like main which I
will call myMain. I know that argv is an array of pointers to strings
It is actually a pointer to a pointer to char but you can treat it as
an array of pointers for most purposes (things like sizeof and & don't
propagate the illusion).
and not an array of strings (elsewise I think it would be hard to
index thru it) but am having trouble setting it up. In my example
below I want argv[0] to point to a command string and would like argv
[1] to point to a string representation of a number. I use sprintf to
make sure the strings are properly built but my program crashes.
While you are guaranteed you can write to the strings pointed to by
argv[i], you are not guaranteed how large those strings are. I would
expect it to depend on the actual arguments provided when the program
is invoked.
I must use C, not C++.
Thanks,
gtb
~~~~~~~~~~~~~~~~~~~~~~~~
void myMain(int argc, char** argv)
{
......
}
void caller(int count;
Did you mean for that ; to be a )?
{
int i, argc;
char* argv[2];
char argv0[32] = {?cmdOne?};
char argv1[32];
argc = 2;
for (i = 0; i < 2; i++)
{
sprintf(argv1, ?%d?, i);
argv[0] = &argv0;
argv[1] = &argv1;
Others have pointed out that the & is incorrect.
myMain(argc, argv);
If you want myMain to work like the standard main does, argv[0] needs
to point to the program name. Is that what "cmdOne" is or is it a
command you expect myMain to execute? Whatever value you assign to
argc, you need to have argv[argc] set to NULL. (This is slightly
counter-intuitive. Normally an array of size x has element from 0 to
x-1. The standard explicitly requires argv to have elements from 0 to
argc with the last set to NULL.)
}
}
--
Remove del for email
.
- References:
- argc & argv
- From: goodTweetieBird
- argc & argv
- Prev by Date: Re: DDE replacement
- Next by Date: Problem with User Fast-Switching
- Previous by thread: Re: argc & argv
- Next by thread: _SCL_SECURE_OUT_OF_RANGE and SetUnhandledExceptionFilter
- Index(es):
Relevant Pages
|