By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,317 Members | 2,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,317 IT Pros & Developers. It's quick & easy.

how to fake argv?

P: n/a
Hi,
is is also a C question but the program im writing is in C++ and it
applies as well so:

i have a program in the "main(int argc, const char ** argv)"
and i want to make a library. for example "myFunc(int argc, const char
** argv)"

i already know what the parameters mean (argc number of params and argv
params field)
The problem is:
Could somebody explain me how to fake a command line?
i'd like for example to be able to pass the parameters:
-x myfile
-f flag1
to the programm.
the commandline would seem like:

"foo.exe -x myfile -f flag1"
but how do i exactly put this together? the params are originally
stored in c++ strings. Thats not the problem since a c++ string can
fake c strings.

my main problem is how to define the argv since its a pointer to a
pointer.
do i have to terminate it? how do i define it? im not that c
experienced. :)

can i say:

char *ptest[5];

ptest[0]="foo.exe";
ptest[1]="-x";
ptest[2]="myfile";
ptest[3]="-f";
ptest[4]="myflag";
char **pptest=ptest;
myFunc(5,ptest);

?
thanks for any help!

Aug 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mw****@freenet.de wrote:
Hi,
....
my main problem is how to define the argv since its a pointer to a
pointer.
do i have to terminate it? how do i define it? im not that c
experienced. :)

can i say:
Yes, this is pretty much how you'd do it - but see below.

char *ptest[5];

ptest[0]="foo.exe";
ptest[1]="-x";
ptest[2]="myfile";
ptest[3]="-f";
ptest[4]="myflag";
char **pptest=ptest;
The above is unneeded. Just pass ptest to myFunc.

Also, argv is supposed to have a NULL pointer as its last element,
so make it one bigger and assign NULL to the last element.
myFunc(5,ptest);
Aug 15 '06 #2

P: n/a
mwebel posted:
how do i define it?
Maybe something like the following:

#ifdef NDEBUG
#define ActualMain main
#else

int main(void)
{
ActualMain(2, (char const*[]){"prog.exe","-k"});
}

#endif

int ActualMain(int argc, char *argv[])
{

}

If you can't use compound literals, you can always define a local array
object.

--

Frederick Gotham
Aug 15 '06 #3

P: n/a
trm
Nils O. Selåsdal schrieb:
mw****@freenet.de wrote:
Hi,
...
my main problem is how to define the argv since its a pointer
to a pointer. do i have to terminate it? how do i define it?
im not that c experienced. :)

can i say:
Yes, this is pretty much how you'd do it - but see below.

char *ptest[5];

ptest[0]="foo.exe";
ptest[1]="-x";
ptest[2]="myfile";
ptest[3]="-f";
ptest[4]="myflag";
char **pptest=ptest;
The above is unneeded. Just pass ptest to myFunc.

Also, argv is supposed to have a NULL pointer as its last
element, so make it one bigger and assign NULL to the
last element.
myFunc(5,ptest);
Easier:

char *ptest[] = {
"foo.exe", "-x", "myfile", "-f", "myflag", NULL
};

myFunc(sizeof ptest/sizeof ptest[0], ptest);

This way, you don't need to bother keeping track of the size of
the array, as the compiler takes care of it for you. (Of course,
if you don't know in advance what the strings are, then you can't
do it like this, but then you would also need to allocate the
array dynamically anyway.)

Aug 15 '06 #4

P: n/a


trm wrote On 08/15/06 10:43,:
Nils O. Selåsdal schrieb:

>>mw****@freenet.de wrote:
>>>Hi,

...

>>>my main problem is how to define the argv since its a pointer
to a pointer. do i have to terminate it? how do i define it?
im not that c experienced. :)

can i say:

Yes, this is pretty much how you'd do it - but see below.
>>>char *ptest[5];

ptest[0]="foo.exe";
ptest[1]="-x";
ptest[2]="myfile";
ptest[3]="-f";
ptest[4]="myflag";
char **pptest=ptest;

The above is unneeded. Just pass ptest to myFunc.

Also, argv is supposed to have a NULL pointer as its last
element, so make it one bigger and assign NULL to the
last element.

>>>myFunc(5,ptest);

Easier:

char *ptest[] = {
"foo.exe", "-x", "myfile", "-f", "myflag", NULL
};

myFunc(sizeof ptest/sizeof ptest[0], ptest);
Neither is quite perfect, because the argv strings
as passed to main() are modifiable but it's undefined
behavior to try modifying the string literals above.
For better fidelity, you could do something like

char p0[] = "foo.exe";
char p1[] = "-x";
char p2[] = "myfile";
char p3[] = "-f";
char p4[] = "myflag";
char *ptest[] = { p0, p1, p2, p3, p4, NULL };

myFunc(sizeof ptest / sizeof ptest[0] - 1, ptest);

Also, note the `-1' in the call: the NULL element is not
included in the argc count.

--
Er*********@sun.com

Aug 15 '06 #5

P: n/a
>i have a program in the "main(int argc, const char ** argv)"
>and i want to make a library. for example "myFunc(int argc, const char
** argv)"

i already know what the parameters mean (argc number of params and argv
params field)
The problem is:
Could somebody explain me how to fake a command line?
i'd like for example to be able to pass the parameters:
-x myfile
-f flag1
to the programm.
the commandline would seem like:

"foo.exe -x myfile -f flag1"
but how do i exactly put this together? the params are originally
stored in c++ strings. Thats not the problem since a c++ string can
fake c strings.
This is comp.lang.c. We don't do fake c strings, we do *REAL* C
strings.

>my main problem is how to define the argv since its a pointer to a
pointer.
Another way of saying it is it's a pointer to an array of pointers.
>do i have to terminate it?
Yes. The pointer after the one for the last argument should be NULL.
(That is, argv[argc] = NULL . This is redundant since you're passing
both argc and the argv array with termination, but C guarantees both
of these).
>how do i define it? im not that c
experienced. :)

can i say:
You're pretty close here.
>char *ptest[5];
See below, this needs to be 6.
>ptest[0]="foo.exe";
ptest[1]="-x";
ptest[2]="myfile";
ptest[3]="-f";
ptest[4]="myflag";
You need:
ptest[5] = NULL;
which also requires that you make the array one larger above.
>char **pptest=ptest;
You don't need pptest (although you can use it if you want).
The address of ptest has type char **, which
matches the argument type for myFunc().
In C89 the declaration of pptest belongs at the beginning of
the function.

>myFunc(5,ptest);

?
Aug 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.