Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > K&R exercise 4-14

Reply
Thread Tools

K&R exercise 4-14

 
 
Kevin Zhou
Guest
Posts: n/a
 
      05-09-2004
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.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      05-09-2004
Kevin Zhou <(E-Mail Removed)> 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;}
 
Reply With Quote
 
 
 
 
Kevin Zhou
Guest
Posts: n/a
 
      05-09-2004
Ben Pfaff wrote:
> Kevin Zhou <(E-Mail Removed)> 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 ?
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      05-09-2004
Kevin Zhou <(E-Mail Removed)> 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;}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-09-2004
Kevin Zhou wrote:
> Ben Pfaff wrote:
>> Kevin Zhou <(E-Mail Removed)> 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)

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      05-09-2004

"Kevin Zhou" <(E-Mail Removed)> 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.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-09-2004
In 'comp.lang.c', Kevin Zhou <(E-Mail Removed)> 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/
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      05-09-2004
In 'comp.lang.c', "Malcolm" <(E-Mail Removed)> 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/
 
Reply With Quote
 
Nick Austin
Guest
Posts: n/a
 
      05-09-2004
On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <(E-Mail Removed)>
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.

 
Reply With Quote
 
Kevin Zhou
Guest
Posts: n/a
 
      05-09-2004
Nick Austin wrote:
> On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <(E-Mail Removed)>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tree functions daily exercise: Range Xah Lee Java 12 06-22-2005 08:51 AM
Cisco Student VPN exercise problem : gen_unrfrag: fail to generate unreachable, unexpected args robert Cisco 0 06-02-2004 07:33 PM
2154 module 4 Exercise 2 Drew Brown MCSE 0 10-22-2003 02:47 AM
Exercise needed for java 2 programmer test lonelyplanet999 Java 1 09-30-2003 10:37 AM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments