views:

139

answers:

3

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?

A: 

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;
mkj
+6  A: 
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);
Keith Randall
.. or just `char *argv[4] = {"fake /path/to/my/program", "fake arg 1", "fake arg 2", NULL}` to save you some real estate.
roe
+2  A: 

A guillotine usually works. Ask the French.

Malfist
if you downvote you're a counter-revolutionary!
Malfist