c使用va_list打印参数列表

Nis*_*911 3 c printf variadic-functions

我有一个简单的参数列表.我只是想把它打印到stdout,但我在打印"结束"之前得到了有线输出.有谁知道空行和不可读的字符来自哪里?

输出:

start
hello
hello2
hello3
hello 4

UH??AWAVAUATE1?S1?H??HH?E?
end



void printTest(const char* msg, ...) {

    va_list ap;
    int i;
    const char* curMsg=0;
    va_start(ap, msg);
    printf("start\n");

    for(curMsg= msg ;  curMsg!=0 ; curMsg = va_arg(ap,  const char*)){
        printf("%s\n", curMsg);
    }
    printf("end\n");
    va_end(ap);
}



int main(){

    printTest("hello", "hello2", "hello3", "hello 4");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

R..*_*R.. 5

当你没有传递一个时,你期望如何读取一个空指针来终止循环?将通话更改为:

printTest("hello", "hello2", "hello3", "hello 4", (char *)0);
Run Code Online (Sandbox Code Playgroud)