On Thu, 8 Jan 2004, Joe H wrote:
Hi i converted a program to a lib. The parameters of main are
maincommand(int argc, char **argv)
I want to use main a s function that takes a string how can i convert
a char * to a char ** or even better how should i substitute the parameters
for a string. thanks
How many parameters, how organized, etc.? For example, if you're
used to calling the old program at the command line like this:
% myprog 100
% myprog 42
% myprog -8192
then you might prototype the new library function as
int myprog(int arg);
But if you write big complicated programs that you call like this:
% myprog -X 310 -Y 100 -bc -g 5
% myprog -X 80 -Y 80 -b -g 10 -k1 1.5 -k2 .3
% myprog -c -k2 50
then you might be better off with a prototype that requires the client
to explicitly state each possible argument's value:
int myprog(int X, int Y, int g, double k1, double k2, int B, int C);
Finally, if you are into clever little parsing algorithms, you
might write programs that get called like this:
% myprog "echome"
% myprog -o foo bar
% myprog recursive -o foo
and then maybe it would make sense to let your library function do
its own splitting-up-of-arguments, and just take a single string
with all the arguments glommed together, like this:
int myprog(const char *all_the_arguments);
It really depends on what you're trying to do.
If you want to pass a single string to a 'main'-like function
that takes (int, char **), you can do it like this:
int callmain(const char *p)
{
int my_argc;
char *my_argv[100];
my_argc = 2;
my_argv[0] = "program name goes here, usually";
my_argv[1] = (char *)p; /* EVIL CAST! EVIL EVIL EVIL! */
my_argv[2] = NULL;
return fake_main(my_argc, my_argv);
}
Notice the presence of an EVIL EVIL EVIL cast to (char *). That's
only necessary because I made 'callmain' take a (const char *)
parameter. I did that only because it seems nicer to the user. It
works as long as 'fake_main' behaves itself and doesn't go modifying
'p' behind our backs. Maybe it would be better to remove the 'const'
altogether... it depends.
HTH,
-Arthur