stdarg问题 [英] stdarg problems
问题描述
有人可以告诉我下面的程序有什么问题吗?
------------- ------------------------------
int main()
{
f1(25 50,测试stdarg。,它正在工作,如果是
工作。);
返回0;
}
void f1(bool条件,const char * msg,...)
{
va_list ap;
va_start(ap,msg);
while(msg!= 0)
{
cout<< msg<< endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
-------------------------------- ------------
输出是
--------------- ---------------------------
测试stdarg。
它正在工作
如果它正常工作。
?
UH ?? AWAVAUATSH ?? 8H?E?
--- ---------------------------------------
谢谢,
NarikNahom
Hi,
Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
--------------------------------------------
the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.
UH??AWAVAUATSH??8H?E?
------------------------------------------
Thanks,
NarikNahom
推荐答案
On Sun,2007年7月1日15:50:50 +0000 ,nariknahom写道:
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
有人能告诉我下面的程序有什么问题吗?
-------------------------------------------
int main()
{
f1(25 50,测试stdarg。,它正在工作,如果它是
工作。");
返回0;
}
voi d f1(bool condition,const char * msg,...)
{
va_list ap;
va_start(ap,msg);
while(msg!= 0)
{
cout<< msg<< endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
-------------------------------- ------------
输出是
--------------- ---------------------------
测试stdarg。
它正在工作
如果它正常工作。
?
UH ?? AWAVAUATSH ?? 8H?E?
--- ---------------------------------------
Hi,
Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
--------------------------------------------
the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.
UH??AWAVAUATSH??8H?E?
------------------------------------------
清理并编译你的代码版本。
#include< cstdarg>
#include< iostream>
void f1(bool condition,const char * msg,...)
{
va_list ap;
va_start(ap,msg);
while(msg!= 0)
{
std :: cout<< msg<< std :: endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
int main()
{
f1(25 50,"测试stdarg。"," it is working,如果它正在工作。,0);
返回0;
}
-
令人讨厌的用户
Cleaned up and compilable version of your code.
#include <cstdarg>
#include <iostream>
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
std::cout<< msg<<std::endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",0 );
return 0;
}
--
Obnoxious User
On Sun,2007年7月1日15:50:50 +0000,nariknahom写道:
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
>
void f1(bool condition,const char * msg,...)
{
va_list ap;
va_start(ap,msg);
while(msg!= 0)
{
cout<< msg<< endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
>
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
您需要知道可以解析多少个参数。你的
代码超出了它的适当环境,进入了未知的内存,因此打印出奇怪的东西。
http://www.cppreference.com/stdother/va_arg.html
>
-
令人讨厌的用户
You need to know how many arguments you can parse. Your
code is stepping outside its proper context and into
uncharted memory, thus printing weird stuff.
http://www.cppreference.com/stdother/va_arg.html
--
Obnoxious User
Obnoxious User写道:
Obnoxious User wrote:
On Sun,2007年7月1日15:50:50 +0000,nariknahom写道:
On Sun, 01 Jul 2007 15:50:50 +0000, nariknahom wrote:
>
有人可以告诉我下面的程序有什么问题?
------------------------------------- ------
int main()
{f1(25 50,测试stdarg。,它正在工作,如果它是
working.");
返回0;
}
void f1(bool condition,const char * msg,...)
{
va_list ap;
va_start(ap,msg);
while(msg!= 0)
{
cout<< msg<< endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
- ------------------------------------------
输出是
------------------------------------------ <测试stdarg。
它正在工作
如果它正在工作。
?
UH ?? AWAVAUATSH ?? 8H?E?
----- -------------------------------------
>Hi,
Can someone tell me what is wrong with the below program?
-------------------------------------------
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is
working." );
return 0;
}
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
cout<< msg<<endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
--------------------------------------------
the ouput is
------------------------------------------
testing stdarg.
it is working
if it is working.
UH??AWAVAUATSH??8H?E?
------------------------------------------
清理并编译你的代码版本。
#include< cstdarg>
#include< iostream>
void f1(bool condition,const char * msg,...)
{
va_list ap;
va_start (ap,msg);
while(msg!= 0)
{
std :: cout<< msg<< std :: endl;
msg = va_arg(ap,const char *);
}
va_end(ap);
}
int main()
{
f1(25 50,"测试stdarg。"," it is working,如果它正在工作。,0);
Cleaned up and compilable version of your code.
#include <cstdarg>
#include <iostream>
void f1(bool condition, const char * msg,...)
{
va_list ap;
va_start(ap, msg);
while (msg != 0 )
{
std::cout<< msg<<std::endl;
msg = va_arg(ap, const char *);
}
va_end(ap);
}
int main()
{
f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",0 );
我相信这应该是
f1(25 50,测试stdarg。,它是工作,,如果它正在工作。,
static_cast< const char *>(0));
保证工作。变量参数函数是我的领域。
原因是你必须在va_arg中使用与调用中相同的类型,并且
也因为整数0不需要将相同的二进制
表示为空指针。
I believe this should be
f1( 25 50,"testing stdarg. ", "it is working", "if it is working.",
static_cast<const char*>(0) );
To make it guaranteed to work. Variable argument functions are mine fields.
The reason is that you must use the same type in va_arg as in the call, and
also because the integer 0 is not required to have the same binary
representation as a null pointer.
返回0;
}
return 0;
}
-
rbh
--
rbh
这篇关于stdarg问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!