Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass list of arguments in function with variable number of parameters?

Tags:

c

printf

printing

I have a string-mask that looks something like this:

                  +--\
                  |   \
                  |    \
              +---|     \
              +---|      \
+                 |       \
|\  +---------------------------------+\
| \ | %d|    %d|    %d|    %d|    %d| | \
|  \| %d|    %d|    %d|    %d|    %d| | |\
|   | %d|    %d|    %d|    %d|    %d| | | \
|---|                                 | |  \
|---|                                 | |  /
|   | %d|    %d|    %d|    %d|    %d| | | /
|  /| %d|    %d|    %d|    %d|    %d| | |/
| / | %d|    %d|    %d|    %d|    %d| | /
|/  +---------------------------------+/
+                 |       /
              +---|      /
              +---|     /
                  |    /
                  |   /
                  +--/

I need to printf it - printf(string-mask, param1,param2,param3, etc...), but number of parameters is a huge (in real string it is about 40). Is there way to avoiding manual enumeration of parameters?

P.S. I'm using pure C.

P.S.S. params are storing in array.

like image 774
0x1337 Avatar asked Jun 02 '16 21:06

0x1337


People also ask

Are arguments passed by value or parameter?

Arguments are Passed by Value. The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations.

How to pass a list as an argument to a function?

Python Passing a List as an Argument ❮ Python Glossary Passing a List as an Argument You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function. E.g. if you send a List as an argument, it will still be a List when it reaches the function:

When to use a variable argument list in Python?

You can use variable argument lists when you need to make a function so general that even the number and types of arguments can vary. The family of functions is an example of functions that use variable argument lists.printfargument-declaration-list.

What are the parameters in a function call?

The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value : The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.


1 Answers

Iterate the array (the string), until you hit a print specifier. Then print the string from where you previously left of, to, including, the specifier, while passing a single argument from the array of values.

This is a quick and dirty solution without error checking that assumes every specifier is exactly %d and there are exactly param_count of them. Also the string must be modifiable.

const size_t param_count = 30;
char* first = string;
char* last = string;
for( size_t i = 0 ; i < param_count ; i++ )
{
    last = strchr( last , '%' ); //find the specifier 
    last += 2 ;  //skip the specifier
    const char temp = *last;
    *last = '\0';  //terminate the 'sub-string'
    printf( first , param[i] );
    *last = temp;   //restore the 'string'
    first = last;
}
printf( first ); //print the remaining string

Here is the output: https://ideone.com/zIBsNj

like image 132
2501 Avatar answered Oct 16 '22 18:10

2501