P 155 k& r第7.3节 [英] P 155 k&r section 7.3

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

问题描述

大家好,

该部分标题为可变长度参数列表。

我可以询问有关使用宏的问题。

引用K& R。


"标准标题< stdarg.h包含一组宏定义

定义如何步骤通过参数列表......类型

va_list用于声明变量...在minprintf..ap

.......宏va_start初始化ap指向第一个未命名的

参数。


任何人都可以帮助我理解使用宏的意义vs

功能,因为我觉得它比我意识到的更重要。


提前感谢。

Hi All,
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontains a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
.......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.

thanks in advance.

推荐答案

mdh< md ** @ comcast.netwrites:
mdh <md**@comcast.netwrites:

该部分标题为可变长度参数列表。

我可以问一个关于使用宏的问题。

引用K& R。


"标准标题< stdarg.h包含一组宏定义

,用于定义如何单步执行参数列表。 ....类型

va_list用于声明变量...在minprintf..ap

......宏va_start初始化ap到点第一个未命名的

参数。


任何人都可以帮助我理解使用宏的意义vs

函数,如我认为它比我意识到的更重要。
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontains a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.



va_start,va_arg,va_end和va_copy被定义为宏的原因

是它们*不能被定义为函数。


例如,va_arg有两个参数,类型为va_list的表达式

(我实际上不确定它是否可以是任意表达式)和一个类型

的名字。它产生命名类型的结果。 AC函数不能将
a类型名称作为参数,只作为一个表达式,它必须返回一个

的结果,该函数是在函数为<时指定的单个类型br />
声明。


现在va_arg不能*可以*被定义为宏 - 但是

那里''总是以某种方式非易移地定义它。在某些情况下,它甚至可能是
,编译器只需为此目的提供一些扩展

,并让va_arg宏使用该扩展;
例如
,实现可能有类似的东西


#define va_arg(ap,type)__ builtin_va_arg __(ap,type)


-

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

诺基亚

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

- Antony Jay和Jonathan Lynn,是部长

The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can''t* be defined as functions.

For example, va_arg takes two arguments, an expression of type va_list
(I''m actually not sure it can be an arbitrary expression) and a type
name. It yields a result of the named type. A C function cannot take
a type name as an argument, only an expression, and it must return a
result of some single type that''s specified when the function is
declared.

Now va_arg can''t *portably* be defined as a macro either -- but
there''s always some way to define it non-portably. In some cases, it
might even be necessary for the compiler to provide some extension
just for the purpose, and have the va_arg macro use that extension;
for example, an implementation might have something like

#define va_arg(ap, type) __builtin_va_arg__(ap, type)

--
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"


mdh写道:
mdh wrote:

大家好,

该部分标题为可变长度参数列表。

我可以问一个关于使用宏。

引用K& R。


"标准头< stdarg.h包含一组宏定义

定义如何单步执行参数列表......类型

va_list用于声明变量...在minprintf..ap中

......宏va_start初始化ap指向第一个未命名的

参数。


任何人都可以帮助我理解使用宏的意义vs

函数,因为我认为它比我意识到的更重要。
Hi All,
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.

"The standard header <stdarg.hcontains a set of macro definitions
that define how to step through an argument list...... The type
va_list is used to declare a variable...in minprintf..ap
......The macro va_start initializes ap to point to the first unnamed
argument."

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.



你不能这样做:


va_arg(arg,int)


带功能。


-

pete

You can''t do this:

va_arg(arg, int)

with a function.

--
pete


9月5日,5:20 * pm,Keith Thompson< ks .. 。@ mib.orgwrote:
On Sep 5, 5:20*pm, Keith Thompson <ks...@mib.orgwrote:

mdh< m ... @ comcast.netwrites:
mdh <m...@comcast.netwrites:

该部分标题为可变长度参数列表。

我可以询问有关使用宏的问题。

引用K& R.
The section is titled Variable-length Argument lists.
May I ask a question about the use of "macros".
To quote K&R.


"标准标题< stdarg.h包含一组宏定义


任何人都可以提供帮助我理解使用宏与

函数的重要性,因为我认为它比我意识到的更重要。
"The standard header <stdarg.hcontains a set of macro definitions

Could anyone help me understand the significance of using macros vs
functions, as I think it is more significant than I realize.



va_start,va_arg,va_end和va_copy被定义为宏的原因

是他们*不能被定义为函数。


例如,va_arg有两个参数,类型为va_list的表达式

(我实际上不确定它是否可以是任意表达式)和一个类型

的名字。 *它产生命名类型的结果。 * AC函数不能把

a类型名作为参数,只有一个表达式,


The reason va_start, va_arg, va_end, and va_copy are defined as macros
is that they *can''t* be defined as functions.

For example, va_arg takes two arguments, an expression of type va_list
(I''m actually not sure it can be an arbitrary expression) and a type
name. *It yields a result of the named type. *A C function cannot take
a type name as an argument, only an expression,




所以,在这个例子中是在p156上给出,


void minprintf(char * fmt,...){


va_list ap;

?这声明了一个类型为va_list的指针?而ap是一个

指针的原因很简单,因为它是定义它的方式??

然后,


va_start (ap,fmt);


所以,根据你的说法,如果我理解这一点,这里的类型(fmt)是

"指向char的指针;表达式''ap''被初始化或返回

作为指向char的指针(到第一个参数)?


并且只是为了确定,这里有一个va_arg使用的例子。

ival被声明为int。


所以表达式


ival = va_arg(ap,int)将返回一个值(ival)

whatever_type ....在本例中为int,但可以是char *,char等?

所以,虽然这些宏中的每一个都做了一些不同的事情,但是通常的线程似乎是参数的类型是

未知,这些宏提供了处理它的机制?



So, in the example that is given on p156,

void minprintf(char *fmt, ...){

va_list ap;

? This declares ap a pointer of type va_list? And the reason ap is a
pointer is simply because that is the way it is defined??
Then,

va_start(ap, fmt);

So, from what you say, if I understand this, the type here (fmt) is
"pointer to char" and the expression ''ap'' is initialized or "returned"
as a pointer to char (to the first argument)?

And just to make sure, an example here of va_arg use.
ival is declared an int.

so the expression

ival=va_arg(ap, int) will return a value ( ival) that is of
whatever_type ....in this case int, but could be char *, char etc?
So, although each of these macros does something a little different,
the common thread seems to be that the type of the argument is
unknown, and these macros provide a mechanism to deal with it?


这篇关于P 155 k&amp; r第7.3节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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