Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-19
6
Medium Priority
?
472 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

604 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