Solved

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

Posted on 2004-10-19
6
453 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:jimdgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12348045
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
 
LVL 5

Accepted Solution

by:
van_dy earned 50 total points
ID: 12348388
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12348495
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 12348553
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
 

Author Comment

by:jimdgar
ID: 12349008
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
 
LVL 5

Expert Comment

by:van_dy
ID: 12350763
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question