如何通过可变数量的参数传递函数中的参数列表?

时间:2021-01-14 11:00:26

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?

我需要printf它 - printf(string-mask,param1,param2,param3等等),但参数数量很大(在实际字符串中它大约是40)。有没有办法避免手动枚举参数?

P.S. I'm using pure C.

附:我正在使用纯C.

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

P.S.S.参数存储在数组中。

1 个解决方案

#1


5  

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.

这是一个快速而肮脏的解决方案,没有错误检查,假设每个说明符都是%d,并且确实存在param_count。字符串也必须是可修改的。

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

以下是输出:https://ideone.com/zIBsNj

#1


5  

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.

这是一个快速而肮脏的解决方案,没有错误检查,假设每个说明符都是%d,并且确实存在param_count。字符串也必须是可修改的。

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

以下是输出:https://ideone.com/zIBsNj