K&R2, exercise 5.3

Discussion in 'C Programming' started by arnuld, Apr 8, 2008.

  1. arnuld

    arnuld Guest

    PURPOSE: write a pointer version of strcat function.

    WHAT I DID: I have 2 implementations of this function. Both compile
    and run but 1st one gives some strange results, 2nd is ok. I am unable
    to figure out why the 1st implementation gives strange results:


    /* A rudimentary strcat function
    *
    */


    #include <stdio.h>
    #include <stdlib.h>


    enum MAXSIZE { ARR_SIZE = 1000 };


    void my_strcat( char s[], char t[] );


    int main()
    {
    char s[1000] = "Saurabh";
    char t[1000] = "Nirkhey\n";

    printf("\ns --> %s\nt --> %s\n\n", s, t );
    printf("--- concatenating strings -----\n\n");
    my_strcat( s, t );
    printf("\ns --> %s\nt --> %s\n\n", s, t );



    return EXIT_SUCCESS;
    }


    void my_strcat( char s[], char t[] )
    {
    char *ps, *pt;

    ps = s;
    pt = t;

    while( *ps++ != '\0' )
    {
    printf("*ps = %c\n", *ps );
    }

    while( *pt != '\0' )
    {
    *ps++ = *pt++;
    }

    }

    =========== OUTPUT ===============

    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
    /home/arnuld/programs/C $ ./a.out

    s --> Saurabh
    t --> Nirkhey


    --- concatenating strings -----

    *ps = a
    *ps = u
    *ps = r
    *ps = a
    *ps = b
    *ps = h
    *ps = ^@



    ==> 2nd version's strcat function:

    void my_strcat( char s[], char t[] )
    {
    char *ps, *pt;

    ps = s;
    pt = t;

    while( *ps != '\0' )
    {
    printf("*ps = %c\n", *ps );
    ps++;
    }

    while( *pt != '\0' )
    {
    *ps++ = *pt++;
    }

    }

    ============ OUPUT ============
    /home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra 5-3.c
    /home/arnuld/programs/C $ ./a.out

    s --> Saurabh
    t --> Nirkhey


    --- concatenating strings -----

    *ps = S
    *ps = a
    *ps = u
    *ps = r
    *ps = a
    *ps = b
    *ps = h

    s --> SaurabhNirkhey

    t --> Nirkhey


    /home/arnuld/programs/C $






    -- http://lispmachine.wordpress.com/

    Please remove capital 'V's when you reply to me via e-mail.
    arnuld, Apr 8, 2008
    #1
    1. Advertising

  2. arnuld schrieb:
    > PURPOSE: write a pointer version of strcat function.
    >
    > WHAT I DID: I have 2 implementations of this function. Both compile
    > and run but 1st one gives some strange results, 2nd is ok. I am unable
    > to figure out why the 1st implementation gives strange results:


    [SNIP]

    > void my_strcat( char s[], char t[] )
    > {
    > char *ps, *pt;
    >
    > ps = s;
    > pt = t;


    Here the pointer ps gets incremented, regardless of the result of
    the comparison. That way, in the loop you print always the character
    *following* the one ps pointed to when the loop condition was last
    checked; the output of your debug print statements should have been
    a hint...

    > while( *ps++ != '\0' )
    > {
    > printf("*ps = %c\n", *ps );
    > }


    At this point, ps points one character *beyond* the terminating null
    character in s. Effectively, you are now going to copy the contents
    of t to "dead"[1] space in the array s:

    > while( *pt != '\0' )
    > {
    > *ps++ = *pt++;
    > }


    [SNIP]

    > ==> 2nd version's strcat function:


    [SNIP]

    This time you got it right: increment ps only if the loop condition is
    true (and debug-print the character ps points to *before* incrementing
    the pointer).

    > while( *ps != '\0' )
    > {
    > printf("*ps = %c\n", *ps );
    > ps++;
    > }

    [SNIP]

    Alternatively, you could have used a for loop, of course:

    for ( ps = s; *ps != '\0'; ++ps )
    /* skip, or whatever */;



    [1] In the sense of: "will be ignored, when s is treated as a string".


    Best regards
    --
    Irrwahn Grausewitz []
    Irrwahn Grausewitz, Apr 8, 2008
    #2
    1. Advertising

  3. arnuld wrote:
    [problem with strcat implementation]
    [first version, buggy:]
    > void my_strcat( char s[], char t[] )
    > {
    > char *ps, *pt;
    >
    > ps = s;
    > pt = t;
    >
    > while( *ps++ != '\0' )
    > {
    > printf("*ps = %c\n", *ps );
    > }
    >
    > while( *pt != '\0' )
    > {
    > *ps++ = *pt++;
    > }
    >
    > }
    >
    > ==> 2nd version's strcat function:

    [working]
    >
    > void my_strcat( char s[], char t[] )
    > {
    > char *ps, *pt;
    >
    > ps = s;
    > pt = t;
    >
    > while( *ps != '\0' )
    > {
    > printf("*ps = %c\n", *ps );
    > ps++;
    > }
    >
    > while( *pt != '\0' )
    > {
    > *ps++ = *pt++;
    > }
    >
    > }


    You have correctly identified the location of the problem. Specifically,
    the problem is the following while loop in the first version:

    while (*ps++ != '\0')

    This says "Check whether *ps equals '\0', and afterwards, increment ps".
    Note that ps is incremented whether or not the equality test succeeds.

    Look at the printf() output. By the time printf() prints *ps, ps has
    already been incremented. This is why you never output the first
    character of the string.

    Similarly, when you detect a '\0' character, you then increment ps past
    that character. As a result, the '\0' is never overwritten with a
    character from pt; the contents of the char array after the call are as
    follows:

    "Saurabh\0Nirkhey\n"

    The '\0' part-way through a char array means "end of string", so the
    printf() call in main() won't print anything beyond that point.

    I hope this helps you understand why your first version doesn't work but
    your second version does.
    Philip Potter, Apr 8, 2008
    #3
    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:
    428
    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,170
    VisionSet
    Sep 30, 2003
  3. Xah Lee
    Replies:
    12
    Views:
    602
    Duncan Booth
    Jun 22, 2005
  4. Bruce .J Sam
    Replies:
    0
    Views:
    1,927
    Bruce .J Sam
    Jun 16, 2005
  5. Aries
    Replies:
    7
    Views:
    400
    Aries
    May 3, 2006
Loading...

Share This Page