K& R练习4-14 [英] K&R exercise 4-14

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

问题描述

问题是定义一个宏交换(t,x,y),它交换两个类型为t的
参数。

这是什么意思?什么是一种类型的arugments?我在书中找不到

的解释。

The question is "define a macro swap(t,x,y) that interchanges two
arguments of type t".
What does it mean? what are arugments of a type? I can''t find an
explanation in the book.

推荐答案

Kevin Zhou< zr **** *@hotmail.com>写道:
Kevin Zhou <zr*****@hotmail.com> writes:
问题是定义一个宏交换(t,x,y),交换两个类型为t的参数。
它是什么意思?什么是一种类型的arugments?我在书中找不到
解释。
The question is "define a macro swap(t,x,y) that interchanges two
arguments of type t".
What does it mean? what are arugments of a type? I can''t find an
explanation in the book.




类型没有参数。宏有三个参数。

第一个(t)是一个类型,第二个和第三个(x和y)是第一个参数指定的类型。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p ,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}



Types don''t have arguments. The macro takes three arguments.
The first (t) is a type, and the second and third (x and y) have
the type specified by the first argument.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Ben Pfaff写道:
Ben Pfaff wrote:
Kevin Zhou< zr ***** @ hotmail.com>写道:

Kevin Zhou <zr*****@hotmail.com> writes:

问题是定义一个交换两个类型为t的参数的宏交换(t,x,y)。
这是什么意思?什么是一种类型的arugments?我在书中找不到
解释。
The question is "define a macro swap(t,x,y) that interchanges two
arguments of type t".
What does it mean? what are arugments of a type? I can''t find an
explanation in the book.



类型没有参数。宏有三个参数。
第一个(t)是一个类型,第二个和第三个(x和y)是第一个参数指定的类型。


Types don''t have arguments. The macro takes three arguments.
The first (t) is a type, and the second and third (x and y) have
the type specified by the first argument.



感谢您的澄清。

这是我在
找到的解决方案 http://users.powernet.co.uk/eton/kandr2/krx414.html


#define swap(t,x,y)do {tz = x; x = y; y = z} while(0)


为什么a while whileloop是必须的吗?


Thanks for the clarification.
Here''s a solution I found at
http://users.powernet.co.uk/eton/kandr2/krx414.html

#define swap(t,x,y) do{t z=x;x=y;y=z}while(0)

why a do while "loop" is necessay ?


Kevin Zhou< zr ***** @ hotmail.com>写道:
Kevin Zhou <zr*****@hotmail.com> writes:
#define swap(t,x,y)do {tz = x; x = y; y = z} while(0)

为什么a while whileloop是必要的吗?
#define swap(t,x,y) do{t z=x;x=y;y=z}while(0)

why a do while "loop" is necessay ?




这是在C FAQ中。


10.4:什么是最好的写作方法多语句宏?


答:通常的目标是编写一个可以被调用的宏,就好像它是一个由单个函数组成的语句呼叫。这个

意味着来电者将提供最终的分号,

所以宏体不应该。因此,宏体不能是一个简单的括号括起来的复合语句,因为语法

如果被调用会产生错误(显然是单个的
$ b) $ b语句,但带有一个额外的分号)作为带有显式else子句的if / else语句的if

分支。


传统解决方案因此,是使用


#define MACRO(arg1,arg2)做{\

/ *声明* / \

stmt1; \

stmt2; \\ /

/ * ... * / \

}而(0)/ *(没有尾随;)* /


当调用者附加分号时,无论上下文如何,此扩展都将变为单个语句。 (优化编译器

将删除任何死测试或常数

条件0上的分支,尽管lint可能会抱怨。)


如果预期宏中的所有语句都是简单的
表达式,没有声明或循环,另一种技术是使用另一种技术来写一个带括号的表达式一个或多个

逗号运算符。 (例如,请参阅问题10.26中的第一个DEBUG()宏

。)此技术还允许值返回

"


参考文献:H& S Sec。 3.3.2 p。 45; CT& P Sec。 6.3 pp.82-3。


-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}



This is in the C FAQ.

10.4: What''s the best way to write a multi-statement macro?

A: The usual goal is to write a macro that can be invoked as if it
were a statement consisting of a single function call. This
means that the "caller" will be supplying the final semicolon,
so the macro body should not. The macro body cannot therefore
be a simple brace-enclosed compound statement, because syntax
errors would result if it were invoked (apparently as a single
statement, but with a resultant extra semicolon) as the if
branch of an if/else statement with an explicit else clause.

The traditional solution, therefore, is to use

#define MACRO(arg1, arg2) do { \
/* declarations */ \
stmt1; \
stmt2; \
/* ... */ \
} while(0) /* (no trailing ; ) */

When the caller appends a semicolon, this expansion becomes a
single statement regardless of context. (An optimizing compiler
will remove any "dead" tests or branches on the constant
condition 0, although lint may complain.)

If all of the statements in the intended macro are simple
expressions, with no declarations or loops, another technique is
to write a single, parenthesized expression using one or more
comma operators. (For an example, see the first DEBUG() macro
in question 10.26.) This technique also allows a value to be
"returned."

References: H&S Sec. 3.3.2 p. 45; CT&P Sec. 6.3 pp. 82-3.

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


这篇关于K&amp; R练习4-14的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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