argv

I have a program which I wish to modify. It used to be run from the command line, but now I wish to change this so it can be used as a function.

The program has complex argument processing so I want to pass my paramters to as if it were being called by the OS as a program.
I have tried to store data in a array and pass it through though it, but hasn't worked in the manner it should.

I thought argv was a char array but this does not seem to be the case.

How should I pass parameters into this function using the argc, argv protocol?

Here is a quicky c program:

#include <stdio.h>

int main(argc, argv)
int argc;
char *argv[];
{
        int i,k;
        printf("argc = %d \n", argc);
        for(i=0; argv; i++) {
                printf("parameter = %d \n", i);
                for(k=0; argv[k]; k++)
                        printf("   char = %c \n", argv[k]);
        }
        exit(0);
}

I can modify it into:

#include <stdio.h>

main()
{

char *argv2[20];
int argc2;
int ret;

argv2[0] = "not_main function";
argv2[1] = "arg one";
argv2[2] = "222";
argv2[3] = NULL;
argc2 = 3;
ret = not_main(argc2, argv2);

/* if not_main uses exit(), we will never get here */
exit(0);

}


int not_main(argc, argv)
int argc;
char *argv[];
{
        int i,k;
        printf("argc = %d \n", argc);
        for(i=0; argv; i++) {
                printf("parameter = %d \n", i);
                for(k=0; argv[k]; k++)
                        printf("   char = %c \n", argv[k]);
        }
        exit(0);
}

(Forgot I posted this one before I went away to my hols!
Why didn't I get an email notification?)

Anyway thanks for the answer Perderabo!

I had assumed that the argv structure was a simple two dimensional array! Doh!

The correct assumption is, of course, that argv is an array of POINTERS!

Once I had sorted that out, everything fell into place!

Thanks again.

MBB