Re: argc & argv

Tech-Archive recommends: Speed Up your PC by fixing your registry



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
.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)
  • Re: reading strings from file
    ... > it opens the file correctly but then the sscanf returns 0, ... those pointers need to point to something. ... strings to random locations in memory. ... in itself an array of char, so to allocate an array of strings, you need ...
    (comp.lang.c)
  • Re: initializing table of strings
    ... >sense to set strings in decleration. ... The fact that it is an array of pointers to const char does not matter ...
    (alt.comp.lang.learn.c-cpp)
  • Re: passing a string to a C++ function
    ... go beyong and array of integers. ... You are right I should revisit pointers since it's been about 7 years since ... > memory management. ... You can't use standatd nul-terminated strings ...
    (microsoft.public.vc.language)
  • Re: Difference between argc and argv?
    ... two parameters (referred to here as argc and argv, ... argvshall be a null pointer. ... If the value of argc is greater than zero, the array members ... to strings, which are given implementation-defined values by the ...
    (alt.comp.lang.learn.c-cpp)