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