![]() |
K&R exercise 4-14
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. |
Re: K&R exercise 4-14
Kevin Zhou <zrubber@hotmail.com> writes:
> 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. 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;} |
Re: K&R exercise 4-14
Ben Pfaff wrote:
> Kevin Zhou <zrubber@hotmail.com> writes: > > >>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. > > > 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. 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 ? |
Re: K&R exercise 4-14
Kevin Zhou <zrubber@hotmail.com> writes:
> #define swap(t,x,y) do{t z=x;x=y;y=z}while(0) > > why a do while "loop" is necessay ? 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;} |
Re: K&R exercise 4-14
Kevin Zhou wrote:
> Ben Pfaff wrote: >> Kevin Zhou <zrubber@hotmail.com> writes: > >>> 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. >> >> 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. > > 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 ? Read the c-faq listed below for the reason. -- Some useful references: <http://www.ungerhu.com/jxh/clc.welcome.txt> <http://www.eskimo.com/~scs/C-faq/top.html> <http://benpfaff.org/writings/clc/off-topic.html> <http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99) |
Re: K&R exercise 4-14
"Kevin Zhou" <zrubber@hotmail.com> wrote in message > > #define swap(t,x,y) do{t z=x;x=y;y=z}while(0) > > why a do while "loop" is necessay ? > This is a really bad exercise. Macros in C are word-processing commands, which means that you have to do lots of tricks to do anything beyond the simple #define constant 123 or an extremely simple function. Even min and max have problems if arguments have side effects, and need lots of ugly parentheses to cope with compound arguments. If we didn't use the do ... while trick we would have a floating {} block, which is illegal. |
Re: K&R exercise 4-14
In 'comp.lang.c', Kevin Zhou <zrubber@hotmail.com> wrote:
> 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. Bad parsing: "define a macro swap(t,x,y) that interchanges two arguments <breath> of type t" -- -ed- get my email here: http://marreduspam.com/ad672570 The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99 FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ |
Re: K&R exercise 4-14
In 'comp.lang.c', "Malcolm" <malcolm@55bank.freeserve.co.uk> wrote:
>> #define swap(t,x,y) do{t z=x;x=y;y=z}while(0) > If we didn't use the do ... while trick we would have a floating {} > block, which is illegal. Wow! Since when has a 'floating block' been illegal? I'm curious. I use it daily to stick to my 'reduce the range of the objects' policy... No, the reason for the do-while trick in macros is to force the use of the final ';' in the source code. Isn't it explained like that in the FAQ? -- -ed- get my email here: http://marreduspam.com/ad672570 The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99 FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ |
Re: K&R exercise 4-14
On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <zrubber@hotmail.com>
wrote: >http://users.powernet.co.uk/eton/kandr2/krx414.html > >#define swap(t,x,y) do{t z=x;x=y;y=z}while(0) I hope that before you submit your coursework you can find and fix the deliberate typo. :) Nick. |
Re: K&R exercise 4-14
Nick Austin wrote:
> On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <zrubber@hotmail.com> > wrote: > > >>http://users.powernet.co.uk/eton/kandr2/krx414.html >> >>#define swap(t,x,y) do{t z=x;x=y;y=z}while(0) > > > I hope that before you submit your coursework you can > find and fix the deliberate typo. :) > > Nick. > Actually I'm learning C from the book for fun. |
| All times are GMT. The time now is 03:59 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.