trying to find the error

Discussion in 'C Programming' started by mdh, May 2, 2008.

  1. mdh

    mdh Guest

    K & R 5-5 asks for a strncat function ( concat n characters of t);

    void mystrncat(char *s, char *t, int n)

    {

    while ( *s++); /* find end of s */ /* <<<<< 1 */

    /* stops at '\0' */ /* <<<<<2 */

    while ( *t && n-- > 0)

    *s++ = *t++; /* /*<<<<< 3 */

    while ( n -- > 0);
    *s++ = '\0';

    }

    Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
    points to '\0'.
    So, when 3 occurs, I thought the first char of t ( *t) is assigned to
    the "Old" position of s, which should be '\0', but it is not.
    What am I missing.
    Thanks in advance.
    mdh, May 2, 2008
    #1
    1. Advertising

  2. mdh

    mdh Guest

    On May 1, 8:22 pm, Eric Sosman <> wrote:
    , but the increment happens regardless of the
    > outcome of the test.


    Ok...I understand. Thank you.
    mdh, May 2, 2008
    #2
    1. Advertising

  3. mdh

    mdh Guest


    >      The loop stops when `s' points at '\0' *before* being
    > incremented, but the increment happens regardless of the
    > outcome of the test.


    May I just ask this. I recently had an extensive explanation about
    this from one of the members of the group. Is this then a fair
    statement about what happens.
    When the loop fails, it is still proceeds to the next sequence point.
    In other words, it is not like a "break" statement in a loop.
    thanks
    mdh, May 2, 2008
    #3
  4. mdh

    Ian Collins Guest

    mdh wrote:
    >> The loop stops when `s' points at '\0' *before* being
    >> incremented, but the increment happens regardless of the
    >> outcome of the test.

    >
    > May I just ask this. I recently had an extensive explanation about
    > this from one of the members of the group. Is this then a fair
    > statement about what happens.
    > When the loop fails, it is still proceeds to the next sequence point.
    > In other words, it is not like a "break" statement in a loop.


    The loop does not fail, the test yields false. The expression "s++" is
    always evaluated before the result of the expression is tested.

    If you want s to point to the end of the string, use

    while( *s ) { s++; }

    --
    Ian Collins.
    Ian Collins, May 2, 2008
    #4
  5. mdh

    mdh Guest

    On May 1, 9:20 pm, Ian Collins <> wrote:

    >
    > The loop does not fail, the test yields false.  The expression "s++" is
    > always evaluated before the result of the expression is tested.
    >


    thank you.
    mdh, May 2, 2008
    #5
  6. mdh

    jt Guest

    On May 2, 9:39 am, mdh <> wrote:
    > On May 1, 9:20 pm, Ian Collins <> wrote:
    >
    >
    >
    > > The loop does not fail, the test yields false. The expression "s++" is
    > > always evaluated before the result of the expression is tested.

    >
    > thank you.


    and moreover the unary operators are right to left associative.since
    here its post incrementation,s is incremented in the next statement.
    probably you can overcome this by using

    while(*s)s++;
    jt, May 3, 2008
    #6
  7. mdh

    Ian Collins Guest

    jt wrote:
    > On May 2, 9:39 am, mdh <> wrote:
    >> On May 1, 9:20 pm, Ian Collins <> wrote:
    >>
    >>
    >>
    >>> The loop does not fail, the test yields false. The expression "s++" is
    >>> always evaluated before the result of the expression is tested.

    >> thank you.

    >
    > and moreover the unary operators are right to left associative.since
    > here its post incrementation,s is incremented in the next statement.


    s++ is the statement, there isn't a next one.

    > probably you can overcome this by using
    >
    > while(*s)s++;


    Which is exactly what I wrote....

    --
    Ian Collins.
    Ian Collins, May 3, 2008
    #7
    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. Bruce D

    trying to find source of error

    Bruce D, Oct 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    452
    jongalloway
    Oct 30, 2004
  2. Rudi
    Replies:
    4
    Views:
    966
  3. Wybo Dekker
    Replies:
    1
    Views:
    352
    Yukihiro Matsumoto
    Nov 15, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    399
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    808
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page