va_arg 读不到最后一个参数,该怎么解决
va_arg 读不到最后一个参数
为什么找不到结束的位置, 读到下一个参数指向的地址信息变成 <读取字符串的字符时出错。>
------解决思路----------------------
------解决思路----------------------
If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted accord-
ing to the default argument promotions), random errors will occur.
va_arg的manpage这样说道,所以楼主不能以va_arg返回NULL作为判断的依据,必须自己传入参数个数。
------解决思路----------------------
execProcedurce("hello", "a",NULL);
最后的NULL必须有
#include <stdio.h>
#include <stdarg.h>
void execProcedurce( /*int cnt, */const char* procedurceName,...)
{
va_list argptr;
va_start( argptr, procedurceName );
while (true)
{
procedurceName = va_arg( argptr, const char* );
if ( procedurceName == NULL)
{
break;
}
procedurceName = NULL;
}
va_end( argptr );
}
int main()
{
execProcedurce("hello", "a");
return 0;
}
为什么找不到结束的位置, 读到下一个参数指向的地址信息变成 <读取字符串的字符时出错。>
------解决思路----------------------
#include <stdio.h>
#include <stdarg.h>
void execProcedurce(int cnt, ...)
{
va_list argptr;
va_start(argptr, cnt);
int i = 0;
while (i < cnt)
{
printf("%s\n", va_arg(argptr, const char*));
i++;
}
va_end(argptr);
}
int main()
{
execProcedurce(2, "hello", "a");
return 0;
}
//hello
//a
------解决思路----------------------
If there is no next argument, or if type is not compatible with the type of the actual next argument (as promoted accord-
ing to the default argument promotions), random errors will occur.
va_arg的manpage这样说道,所以楼主不能以va_arg返回NULL作为判断的依据,必须自己传入参数个数。
------解决思路----------------------
execProcedurce("hello", "a",NULL);
最后的NULL必须有