Question

I am trying to move some code from a separate binary and have it inside my main program. Unfortunately I can't mimic the initialization variables for the main function.

How can I create argc and argv by hand? Can someone give me some example assignments.

since it looks like this:

int main(int argc, char *argv[])

I figured I could assign them like this:

int argc=1;
char *argv[0]="Example";

But it doesn't work. Can anyone tell me how this might be done?

Was it helpful?

Solution

int argc = 3;
char *argv[4];
argv[0] = "fake /path/to/my/program";
argv[1] = "fake arg 1";
argv[2] = "fake arg 2";
argv[3] = NULL;
fakemain(argc, argv);

OTHER TIPS

The last element of the argv[] array is actually argv[argc] which is a NULL pointer.

Some example code:

char *argv[] = { "first", "second", NULL }; 
int argc = sizeof(argv) / sizeof(*argv) - 1;

Why should you do that? If you use an IDE then it'll have an option to add command line option. If you're calling the program directly then just call it with the command options

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top