如何解决c使用va_list打印参数列表
| 我有一个简单的参数列表。而且我只是想将其打印到标准输出,但是在打印“结束”之前我要获得有线输出。有谁知道空行和不可读字符来自何处? 输出: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;
}
解决方法
当您未传递空指针时,您如何期望读取空指针来终止循环?将呼叫更改为:
printTest(\"hello\",\"hello2\",\"hello3\",\"hello 4\",(char *)0);
, va_list列表不是以NULL结尾的。实际上,它不提供有关有多少个参数的任何信息。您的论点必须表明存在多少个论点。例如,对于printf(),format参数指示要处理的其他参数的数量。
如果您需要列表以NULL结尾,则需要传递NULL作为最后一个参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。