|
发表于 2020-1-30 11:31:35
|
显示全部楼层
这个问题不用宏是无解的,因为你不提供任何参数个数信息的话函数本身是无法确定参数到底占据了多大的栈空间。
不过你倒是可以考虑使用宏来一定程度上解决这个问题:- #include <stdio.h>
- #include <stdarg.h>
- void __getstr(int n, ...) {
- printf("一共接受到%d个字符串\n",n);
- va_list iter;
- va_start(iter, n);
- for (int i = 0; i < n; ++i) {
- printf("%s\n", va_arg(iter,const char*));
- }
- va_end(iter);
- }
- #define EXPAND(...) __VA_ARGS__
- #define CPARAM10(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,argnum,...) argnum
- #define CPARAM(...) EXPAND(CPARAM10(__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0))
- #define getstr(...) __getstr(CPARAM(__VA_ARGS__),__VA_ARGS__)
- int main() {
- getstr("one", "two", "three");
- getstr("hello world");
- system("pause");
- }
复制代码 |
|