M P
M P

Reputation: 362

trouble in passing character pointer array as argument

I am just learning objective C and got an assignment to wrap C library to invoke via objective C. I am having trouble in passing character pointer array to c function. Basically i am invoking main method of c program via objective c but unable to pass arguments. Below is the function that i am trying to invoke from objective c:

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

where argv is the command line arguments passed from console but now i want to pass those arguments from objective c code. Command line syntax of C program is : ./test -karg1 -larg2 -rarg3....

Please help me how can i invoke it via objective C (for sure i need to learn Pointer in C).

Regards, MP

Upvotes: 0

Views: 422

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

It is not too hard, and you do not need to know much about pointers:

// Initialize an array of four constant C strings
// (that is, pointers to zero-terminated char arrays)
const char* argv[] = {"./test", "-karg1", "-larg2", "-rarg3"};
// Call your test function
int res = test_main(4, argv);

Note that the initial argument (at position 0) is the name of the program. That is what C programs expect: their first "actual" argument is at position 1.

Upvotes: 1

Related Questions