Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   K&R exercise 4-14 (http://www.velocityreviews.com/forums/t318179-k-and-r-exercise-4-14-a.html)

 Kevin Zhou 05-09-2004 02:55 AM

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.

 Ben Pfaff 05-09-2004 03:04 AM

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;}

 Kevin Zhou 05-09-2004 03:19 AM

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 ?

 Ben Pfaff 05-09-2004 03:33 AM

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;}

 CBFalconer 05-09-2004 03:47 AM

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)

 Malcolm 05-09-2004 08:38 AM

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.

 Emmanuel Delahaye 05-09-2004 09:44 AM

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.

"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
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

 Emmanuel Delahaye 05-09-2004 09:50 AM

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
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

 Nick Austin 05-09-2004 12:28 PM

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.

 Kevin Zhou 05-09-2004 08:23 PM

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 04:04 PM.