how to fake argv?

M

mwebel

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!
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

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.
 
F

Frederick Gotham

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.
 
T

trm

Nils said:
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.)
 
E

Eric Sosman

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

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.
 
G

Gordon Burditt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top