没有格式字符串传递给变量参数列表函数 [英] No format string passed to variable argument list function

查看:78
本文介绍了没有格式字符串传递给变量参数列表函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个简单的类似printf的功能:


int print(const char * format,...)

{

char buffer [1024];

va_list argptr;

int i;


va_start(argptr,format);

i = vsnprintf(buffer,sizeof(buffer),format,argptr);

va_end(argptr);


buffer [sizeof(buffer)-1] =''\ 0'';


printf("%s \ N'QUOT;,缓冲液); / *这一点只是为了测试* /


返回i;

}


如果我使用以下内容调用该函数:

char message [50];

strcpy(message,hi there);

print( %s,消息);


一切正常,但如果我这样做:

打印(消息);


它没有(程序因中止而崩溃)。


我正在做什么有什么问题,或者我应该看看

在其他地方解决我的崩溃原因?


非常感谢,

Adam

Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr, format);
i = vsnprintf(buffer, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = ''\0'';

printf("%s\n",buffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",message);

everything works, but if I do:
print(message);

it doesn''t (program crashes with an abort).

Is there something wrong with what I''m doing, or should I be looking
elsewhere to work out the cause of my crash?

Thanks a lot,
Adam

推荐答案

Adam< ne ** @ snowstone.org.ukwrites:
Adam <ne**@snowstone.org.ukwrites:




我有一个类似printf的简单函数:


int print(const char * format,...)

{

char buffer [1024];

va_list argptr;

int i;


va_start(argptr,format) ;

i = vsnprintf(缓冲区,sizeof(缓冲区),格式,argptr);

va_end(argptr);


缓冲区[sizeof(buffer)-1] =''\ 0'';


printf("%s \ n",buffer); / *这一点只是为了测试* /


返回i;

}


如果我使用以下内容调用该函数:

char message [50];

strcpy(message,hi there);

print( %s,消息);


一切正常,但如果我这样做:

打印(消息);


它没有(程序因中止崩溃)。
Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr, format);
i = vsnprintf(buffer, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = ''\0'';

printf("%s\n",buffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",message);

everything works, but if I do:
print(message);

it doesn''t (program crashes with an abort).



我自己没有看到问题。我接受了你的功能并添加了一个主要功能:


#include< stdarg.h>

#include< stdio.h>


/ *你的功能* /


int main(无效){

print("%s \ n" ,Hello world!;

print(Goodbye world!\ n);

返回0;


它在三个不同的系统上编译并运行没有问题。


但是,可能会有一些我不想要的东西,这些系统上没有显示

。你可以发布一个完整的程序中止,告诉我们你使用的系统和编译器吗?


看起来v * printf应该是能够处理来自

a函数的va_list而没有额外的参数,尽管标准似乎并没有明确地说明。可以想象你的

标准库中有一个错误,但很难说。

I don''t see a problem myself. I took your function and added a main function:

#include <stdarg.h>
#include <stdio.h>

/* your function */

int main(void) {
print("%s\n", "Hello world!");
print("Goodbye world!\n");
return 0;
}

and it compiles and runs without problems on three different systems.

There could be something that I''m missing, though, which doesn''t show up
on those systems. Can you post a complete program that aborts, and tell
us the system and compiler you''re using?

It certainly seems like v*printf should be able to handle a va_list from
a function with no extra arguments, though the standard doesn''t
explicitly seem to say so. It''s conceivable that there''s a bug in your
standard library, but hard to say just yet.


Adam< ne **@snowstone.org.ukwrites:
Adam <ne**@snowstone.org.ukwrites:

我有一个类似printf的简单函数:


int print(const char *格式,...)

{

char buffer [1024];

va_list argptr;

int i;


va_start(argptr,format);

i = vsnprintf(buffer,sizeof(buffer),format,argptr);

va_end(argptr);


buffer [sizeof(buffer)-1] =''\''';
I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr, format);
i = vsnprintf(buffer, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = ''\0'';



这不是你的问题,但上面的目的是什么?

vsnprintf()应该已经写了一个''\''' - 终止缓冲区的字符串;

你只是设置字符1023(这可能远远超出了字符串的结尾
)到''\'''。

This isn''t your problem, but what''s the purpose of the above?
vsnprintf() should already write a ''\0''-terminated string to buffer;
you''re just setting character 1023 (which is likely far beyond the end
of the string) to ''\0''.


printf("%s \ n",buffer); / *这一点只是为了测试* /


返回i;

}


如果我使用以下内容调用该函数:

char message [50];

strcpy(message,hi there);

print( %s,消息);


一切正常,但如果我这样做:

打印(消息);


它没有(程序因中止而崩溃)。


我正在做什么有什么问题,或者我应该看看

在其他地方解决我的崩溃原因?
printf("%s\n",buffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",message);

everything works, but if I do:
print(message);

it doesn''t (program crashes with an abort).

Is there something wrong with what I''m doing, or should I be looking
elsewhere to work out the cause of my crash?



如果您可以发布一个小的,完整的,可编译的程序

来证明问题,这将有所帮助。我没有看到你发布的任何内容,这些都解释了你所看到的症状。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

It would help if you could post a small, complete, compilable program
that demonstrates the problem. I don''t see anything in what you
posted that explains the symptom you''re seeing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Adam写道:
Adam wrote:




我有类似printf的简单功能:
Hi,

I have a simple printf-like function:



您是否包含< stdarg.h>?


-

Ian Collins

Did you include <stdarg.h>?

--
Ian Collins


这篇关于没有格式字符串传递给变量参数列表函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆