Solved

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

Posted on 2004-10-19
6
442 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now