# C : vprintf an array



## yom (Apr 18, 2013)

Hello,

I've been looking for a way in C to do something like this :


```
char *args[] = { "arg1", "arg2", "arg3", NULL };
somefunc("%s %s %s", args);
```

I only require this to work on an array of strings. I've read that trying to build a va_list is somewhat close to black magic since it's machine dependent. I've been able to have a look at the va_list memory and been able to find back arguments in it with a real va_list function like :


```
somefunc("%s %s %s", "arg1", "arg2", "arg3", NULL);
```

It's not that difficult to understand how it works, but since I need this to be portable and fast. It's a bit out of my current knowledge of C. There's always the "lame" version that would do something like


```
somefunc("%s %s %s", args, count);
```

and to process this through a


```
switch(count) {
    case 1: printf(fmt, args[0]); break;
    ...
}
```

But since I don't know how many arguments I would get in the array of args, there's always some kind of drawback.

Any advice please?


----------



## NewGuy (Apr 18, 2013)

Maybe I am missing part of the problem, but I don't see why you can't just use the code in your first example where you pass in an array of strings to your function. The function itself can move through the array and simply stop when it hits the NULL value.


```
void my_function (char *string, char **args)
{
   int index = 0;

   while ( args[index] )
   {
        // do something here with args[index]
        index++;
   }
}
```

As long as all of your variables being passed in are of the same type the above example should work just fine. You don't need to pass in an array counter as long as the final element of the array is set to NULL.


----------



## kpa (Apr 18, 2013)

The way this is done in C functions like printf(3) that take variable number of arguments is that you parse the format string by the format specifiers (the percent signs and what follows them) and then look up the arguments for each specifier using the va_* macros.

I would however try any other approach because functions that take variable number of arguments are just disaster waiting to happen.


----------



## fonz (Apr 18, 2013)

yom said:
			
		

> since I don't know how many arguments I would get in the array of args, there's always some kind of drawback.


As said by @NewGuy: as long as you can muster the discipline to terminate the array with a NULL pointer, you can just pass the array itself. Another approach would be to use a linked list, but that might be overkill in this particular situation. 

Using variadic functions is fine as long as you know what you're doing, but in my experience most of the time when you _think_ you need them, you don't because there's an easier way. In fact, come to think of it, the only case in which I've ever (usefully) used them was for multi-level wrappers around printf(3) and friends.


----------



## yom (Apr 18, 2013)

Thanks for your answers, I've found a limited and "standard" way to do this.
- limited: to the use I've planned of the function, plus it will be static in a library so that it can't be used in other ways that are not planned to work with it.
- standard: format string handling (easy in the planned cases).

When the idea came first through my mind I wanted to use existing behaviors and not to make a new wheel.
But now in this case I guess it was better to create the wheel again


----------



## nslay (Apr 20, 2013)

If you plan to have a format string, why not just expand the array elements yourself? A format string written by a human would never be impractically long. There's nothing wrong with the following

```
printf("%s %s %s", arg[0], arg[1], arg[2]);
```
If you ever did intend to write a format string with tens and tens of arguments, then why not just use a loop? Your array assumption implies every element is the same type.


----------

