[英]How could I use scanf in a function?
void mscanf(char *format, ...)
{
scanf(format);
}
int main()
{
int n1, n2;
mscanf("%d%d", &n1, &n2);
printf("%d,%d\n", n1, n2);
return 0;
}
what's wrong with this.? 这是怎么了? How could I use scanf in a function just like this?
我该如何在函数中使用scanf? thanks.
谢谢。
Instead of your scanf
you want vscanf
: 您需要使用
vscanf
而不是scanf
:
va_list args;
va_start(args, format);
vscanf(format, args);
va_end(args);
When you use functions with variable arguments list you sometimes stumble on the need to pass those variable arguments . 当您将函数与可变参数列表一起使用时,有时会偶然发现需要传递那些可变参数 。 So in your example you know what you want to do, you want to "pass the '...' to the scanf".
因此,在您的示例中,您知道要执行的操作,您想“将'...'传递给scanf”。 That's exactly what the
vscanf
, vprintf
etc functions are for: they accept the '...' in the form of a va_list
. 这正是
vscanf
, vprintf
等函数的用途:它们接受va_list
形式的“ ...”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.