va_arg问题 [英] va_arg problem

查看:146
本文介绍了va_arg问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧


我的c有点生疏,我的大脑已经死了!


谁能告诉我为什么以下的我打电话打印

pferror(E1); ...


#include< stdio.h>

#include< stdarg.h>


#define UE 0

#define E1 1

#define E2 2

#define E3 3

#定义E4 4

char * error_msg [] = {

" Undefined error",

" Error msg 1" ,

错误消息2,

错误消息3,

"错误消息4,

};


/ *打印格式错误* /

void pferror(int,...);


int

main()

{

pferror(E1);

pferror(E2) ,Msg(E2));

pferror(E3,Msg(E3)(%d),111);

pferror(E4 ,Msg(E4)(%d)(%s),222,三);

返回0;

}

void

pferror(int error,...)

{

va_list args;

char * fmt;


fprintf(stderr,"错误:%s",error_ msg [错误]);


va_start(args,错误);

fmt = va_arg(args,char *);

if(fmt)

vfprintf(stderr,fmt,args);

va_end(args);

fprintf(stderr," \\ \\ n");

}

解决方案

任何人都可以告诉我为什么以下版本打印时我叫


pferror(E1); ...



注意应该说......试着打电话给vfprintf ...(肯定是大脑

死天!)<马克写道:


我的c有点生疏,我的大脑已经死了!


任何人都可以告诉我为什么我打电话时打印下面的试图

pferror(E1); ...



因为你有未定义的行为。


int

main()

{

pferror(E1);



(fx:snip)


void

pferror(int error ,...)

{

va_list args;

char * fmt;


fprintf (stderr,错误:%s,error_msg [错误]);


va_start(args,错误);

fmt = va_arg(args ,char *);

if(fmt)



没有第二个参数,因此`va_arg`的结果未定义。

如果没有相应的

参数,
`va_arg`没有指定返回null - 怎么可能,因为你可以请求'int`或`float`

没有空值的args返回?


这取决于你/知道/是否有应该是va_arguments。


-

Chris" C不是Lisp / Python / Ruby / Spice / etc" Dollin


Hewlett-Packard Limited注册办公室:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN


Mark< mj ***** @ gmail.comwrote:


任何人都可以告诉我为什么下面的试图打印当我打电话给

pferror(E1); ...


#include< stdio.h>

#include< stdarg.h>


/ *打印格式错误* /

void pferror(int,...);


int

main()

{

pferror(E1);


}

void

pferror(int error,...)

{

va_list args;

char * fmt;


fprintf(stderr," Error:% s",error_msg [error]);


va_start(args,error);

fmt = va_arg(args,char *);

if(fmt)



因为要求va_arg()获取一个不存在的变量参数

导致未定义的行为,不是空值。你可以通过两种方式解决这个问题:

- 总是传递一个额外的参数,告诉你是否有

变量参数,如果有的话,多少;

- 在没有变量参数的特定情况下,传递额外的

显式空指针(即,做pferror(E1,(char *)0 );)。

第一种选择是更多的工作,但更加一致;第二个选项

引入了一个特殊情况,但仅限于特殊情况。


Richard

Hi all

My c is a bit rusty and my brain is dead!

Can anyone tell me why the following trys to print when I call
pferror(E1); ...

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

#define UE 0
#define E1 1
#define E2 2
#define E3 3
#define E4 4

char *error_msg[] = {
"Undefined error",
"Error msg 1",
"Error msg 2",
"Error msg 3",
"Error msg 4",
};

/* Print formatted error */
void pferror (int, ...);

int
main ()
{
pferror(E1);
pferror(E2, "Msg (E2)");
pferror(E3, "Msg (E3) (%d)", 111);
pferror(E4, "Msg (E4) (%d)(%s)", 222, "Three");
return 0;
}
void
pferror (int error, ...)
{
va_list args;
char *fmt;

fprintf(stderr, "Error: %s ", error_msg[error]);

va_start(args, error);
fmt = va_arg(args, char *);
if(fmt)
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}

解决方案

Can anyone tell me why the following trys to print when I call

pferror(E1); ...

Note that should say ... trys to call vfprintf ... (definately a brain
dead day!)


Mark wrote:

My c is a bit rusty and my brain is dead!

Can anyone tell me why the following trys to print when I call
pferror(E1); ...

Because you have undefined behaviour.

int
main ()
{
pferror(E1);

(fx:snip)

void
pferror (int error, ...)
{
va_list args;
char *fmt;

fprintf(stderr, "Error: %s ", error_msg[error]);

va_start(args, error);
fmt = va_arg(args, char *);
if(fmt)

There''s no second argument, so the result of `va_arg` is undefined.

`va_arg` isn''t specified to return null if there''s no corresponding
argument -- how could it be, since you can ask for `int` or `float`
args with no null value to return?

It''s up to you to /know/ if there''s supposed to be va_arguments.

--
Chris "C is not Lisp/Python/Ruby/Spice/etc" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


Mark <mj*****@gmail.comwrote:

Can anyone tell me why the following trys to print when I call
pferror(E1); ...

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

/* Print formatted error */
void pferror (int, ...);

int
main ()
{
pferror(E1);

}
void
pferror (int error, ...)
{
va_list args;
char *fmt;

fprintf(stderr, "Error: %s ", error_msg[error]);

va_start(args, error);
fmt = va_arg(args, char *);
if(fmt)

Because asking va_arg() for a variable argument which isn''t there
results in undefined behaviour, not in a null value. You can solve this
in two ways:
- always pass an extra argument which tells you whether there are
variable arguments, and if so, how many;
- in the specific case of no variable arguments only, pass an extra
explicit null pointer (i.e., do pferror(E1, (char *)0); ).
The first option is more work, but more consistent; the second option
introduces a special case, but is more work only for that special case.

Richard


这篇关于va_arg问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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