Solved

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

Posted on 2004-10-19
6
448 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
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 how to use strings and some functions related to them in the C programming language.

805 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