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.
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.
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:
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With