我想从我的可变参数函数输入值中获取一个字符串 (const char*)。但是下面的这段代码不起作用.....运行时错误是结果
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);
有任何想法吗?