0
我想从我的可变参数函数输入值中获取一个字符串(const char *)。 但下面这段代码不行.....运行时错误是结果C可变参数函数如何从输入获取字符串
void print(const char fmt[], ...) {
va_list ap;
const char *p=fmt;
va_start(ap,fmt);
while(*p) {
if(*p == '%') {
p ++;
if (*p == 'i') {
int num = va_arg(ap, int);
fprintf(output, "%d", num);
} else if (*p == 'f') {
float num = va_arg(ap, float);
fprintf(output, "%f", num);
} else if (*p == 's') {
const char* str = va_arg(ap, const char*);
fprintf(output, "%s", str);
} else
break;
p ++;
} else
break;
}
va_end(ap);
}
// This is how I call the function:
print("%s%f", "Num: ", 12.34);
任何想法?
什么样的运行时错误? – arnaud576875
请更多地显示该函数的代码,以及您如何调用它。 – Mat
为什么不直接调用fprintf' – ThiefMaster