• C

How do I define an array to mimic argv[]?

This should be easy but I can't figure it out.  I want to define an array which can be passed just like argv[].  Example code:

main (int argc, char *argv[])
{
   /* initialization */
   do_some_stuff (argc,argv);
}

void do_some_stuff (int argc, char *argv[])
{
   ...
}

Now I want to call do_some_stuff from elsewhere in the program with internally defined arguments.

void some_function ()
{
   int targc=0;
   char targv[10][128];

   strcpy (targv[0],"string1");  ++targc;
   strcpy (targv[1],"string2");  ++targc;
   ...etc.

   do_some_stuff (targc,targv);   <---- compiler gives me an incompatible pointer type warning.
jimdgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
argv is an array of pointers to char, it is not the same to a bidimensional char array.
If you want to pass your array, you have to declare your function as:

do_some_stuff (int targc, char targv[][128]);

Also you can replace the 128 with a macro value.
 
0
van_dyCommented:
Replace

  char targv[10][128];

in some_funt with:
/*---------------------*/
   char *targv[10];

   for(i = 0; i < 10; ++i)
         targv[i] = malloc(128);
/*---------------------*/

hope this helps;
van_dy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
Consider that in van_dy alternative you have to free the allocated buffers somewhere at proper time, with:

for(i = 0; i < 10; ++i)
        free(targv[i]);

Also you can define a monolithic buffer and point to a portion of it, this way:

void some_function ()
{
   int targc=0;
   char *targv[10];   /* similar to argv */
   char buffer[10*128];   /* monolithic buffer */

   for(i = 0; i < 10; ++i)
        targv[i] = buffer + 128*i;

   strcpy (targv[0],"string1");  ++targc;
   strcpy (targv[1],"string2");  ++targc;
   ...etc.

   do_some_stuff (targc,targv);

In this case you don't have to "free" the buffer
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

stefan73Commented:
Hi jimdgar,
Instead of defining main as
(int argc, char *argv[])
better use
(int argc, char **argv)

...this you can use nicely for functions.
Cheers!

Stefan
0
jimdgarAuthor Commented:
Jaime, your 1st solution simply transferred the compiler warning to main ().
van_dy's solution works fine with the caveat that you should free the allocated space later.
Stefan's appeared to be the most elegant solution but probably over my head; I could not make it work.

Thanks
0
van_dyCommented:
Hi,

   Jaime's reccomendation to using a monolithic buffer is undoubtedly elegant.
One doesn't need to free the allocated space, since its in the functions's stack.
yet another way to  do that would be to use the alloca() function call which allocates
space in the stack of the function making the alloca call itself;  as an example:

void some_function ()
{
   int targc=0;
   char *targv[10];

   for(i = 0; i < 10; ++i)
        targv[i] = (char *)alloca(128);

   strcpy (targv[0],"string1");  ++targc;
   strcpy (targv[1],"string2");  ++targc;
 
van_dy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.