K&R exercise 4-14

Discussion in 'C Programming' started by Kevin Zhou, May 9, 2004.

  1. Kevin Zhou

    Kevin Zhou Guest

    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, May 9, 2004
    #1
    1. Advertising

  2. Kevin Zhou

    Ben Pfaff Guest

    Kevin Zhou <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, May 9, 2004
    #2
    1. Advertising

  3. Kevin Zhou

    Kevin Zhou Guest

    Ben Pfaff wrote:
    > Kevin Zhou <> 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 ?
    Kevin Zhou, May 9, 2004
    #3
  4. Kevin Zhou

    Ben Pfaff Guest

    Kevin Zhou <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, May 9, 2004
    #4
  5. Kevin Zhou

    CBFalconer Guest

    Kevin Zhou wrote:
    > Ben Pfaff wrote:
    >> Kevin Zhou <> 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)
    CBFalconer, May 9, 2004
    #5
  6. Kevin Zhou

    Malcolm Guest

    "Kevin Zhou" <> 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.
    Malcolm, May 9, 2004
    #6
  7. In 'comp.lang.c', Kevin Zhou <> 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/
    Emmanuel Delahaye, May 9, 2004
    #7
  8. In 'comp.lang.c', "Malcolm" <> 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/
    Emmanuel Delahaye, May 9, 2004
    #8
  9. Kevin Zhou

    Nick Austin Guest

    Nick Austin, May 9, 2004
    #9
  10. Kevin Zhou

    Kevin Zhou Guest

    Nick Austin wrote:
    > On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <>
    > 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.
    Kevin Zhou, May 9, 2004
    #10
  11. On Sun, 09 May 2004 16:23:35 -0400, Kevin Zhou <>
    wrote:

    >Nick Austin wrote:
    >> On Sat, 08 May 2004 23:19:51 -0400, Kevin Zhou <>
    >> 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)

    ^;

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

    SWAP(INT, x, y);
    RoSsIaCrIiLoIA, May 10, 2004
    #11
  12. Kevin Zhou <> wrote in message news:<c7k6ko$d59$>...
    > 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.


    It means that the two arguments x and y have an undetermined type t.

    I wrote some of the different macros on Richard Heathfield's site.
    Here's a different solution I thought of that uses sizeof, so it's
    category 1:

    #define swap(t,x,y) myswap(sizeof(t),(unsigned char*)&(x),(unsigned
    char*)&(y))
    void myswap(unsigned long a,unsigned char *b,unsigned char *c)
    {
    while (a--) {
    *b ^= *c;
    *c ^= *b;
    *b++ ^= *c++;
    }
    }

    Gregory Pietsch
    Gregory Pietsch, May 13, 2004
    #12
  13. Kevin Zhou

    Sam Dennis Guest

    Gregory Pietsch wrote:
    > void myswap(unsigned long a,unsigned char *b,unsigned char *c)


    Not size_t a?

    > {
    > while (a--) {
    > *b ^= *c;
    > *c ^= *b;
    > *b++ ^= *c++;
    > }
    > }


    That's a really bad idea; not only does it fail to deal with the two
    values being the same, but, should they share a single byte of their
    respective representations, it has undesirable behaviour for the few
    types where it's undefined at all.

    --
    ++acr@,ka"
    Sam Dennis, May 14, 2004
    #13
  14. Sam Dennis <> wrote in message news:<-berlin.de>...
    > Gregory Pietsch wrote:
    > > void myswap(unsigned long a,unsigned char *b,unsigned char *c)

    >
    > Not size_t a?


    It probably comes later on in K&R2, or was a brain cramp on my fault. ;-)

    >
    > > {

    if (b != c)
    > > while (a--) {
    > > *b ^= *c;
    > > *c ^= *b;
    > > *b++ ^= *c++;
    > > }
    > > }

    >
    > That's a really bad idea; not only does it fail to deal with the two
    > values being the same, but, should they share a single byte of their
    > respective representations, it has undesirable behaviour for the few
    > types where it's undefined at all.


    Then how would you actually accomplish this?
    Gregory Pietsch, May 15, 2004
    #14
  15. Kevin Zhou

    Sam Dennis Guest

    Gregory Pietsch wrote:
    > Sam Dennis <> wrote in message news:<-berlin.de>...
    >> Gregory Pietsch wrote:
    >> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)

    >>
    >> That's a really bad idea;

    >
    > Then how would you actually accomplish this?


    The same way as elsewhere in this thread (with intentional and
    unintentional mistakes corrected): with a temporary variable.

    --
    ++acr@,ka"
    Sam Dennis, May 16, 2004
    #15
  16. Sam Dennis <> wrote in message news:<-berlin.de>...
    > Gregory Pietsch wrote:
    > > Sam Dennis <> wrote in message news:<-berlin.de>...
    > >> Gregory Pietsch wrote:
    > >> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)
    > >>
    > >> That's a really bad idea;

    > >
    > > Then how would you actually accomplish this?

    >
    > The same way as elsewhere in this thread (with intentional and
    > unintentional mistakes corrected): with a temporary variable.


    So here's what you're telling me:

    If the areas pointed to by b and c overlap, I should use a temporary
    variable to swap the contents of b and c:

    void myswap(size_t a, unsigned char *b, unsigned char *c)
    {
    unsigned char uc;

    if (a && b && c)
    while (a--) {
    uc = *b;
    *b = *c;
    *c = uc;
    b++, c++;
    }
    }

    My point is that if the two areas overlap, you can't swap no matter
    what algorithm you use. (Think about it for a second.)

    Gregory Pietsch
    Gregory Pietsch, May 21, 2004
    #16
  17. Kevin Zhou

    Sam Dennis Guest

    Gregory Pietsch wrote:
    >> > Sam Dennis <> wrote in message news:<-berlin.de>...
    >> >> Gregory Pietsch wrote:
    >> >> > void myswap(unsigned long a,unsigned char *b,unsigned char *c)
    >> >> > [xor bytewise]
    >> >>
    >> >> That's a really bad idea;

    >
    > So here's what you're telling me:
    >
    > If the areas pointed to by b and c overlap, I should use a temporary
    > variable to swap the contents of b and c:


    Where did you get that from? What I'm trying to say is that the xor
    hack is unreliable under the best of circumstances and that applying
    it to object representations (of an unknown type, no less) is asking
    for trouble.

    If you absolutely need a function of this sort (for swapping arrays,
    or somesuch, I guess), it's probably most efficient to have a buffer
    local to the function of a reasonable size for a loop of three calls
    to memcpy, analogous to the three assignments in a swapping macro.

    What the hell would swapping objects that overlap mean, anyway? It's
    utter nonsense!

    --
    ++acr@,ka"
    Sam Dennis, May 21, 2004
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer
    Replies:
    2
    Views:
    439
    John Saunders
    Aug 6, 2003
  2. lonelyplanet999

    Exercise needed for java 2 programmer test

    lonelyplanet999, Sep 30, 2003, in forum: Java
    Replies:
    1
    Views:
    4,182
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    607
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,947
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    404
    Aries
    May 3, 2006
Loading...

Share This Page