trying to find the error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mdh

    trying to find the error

    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.

  • Eric Sosman

    #2
    Re: trying to find the error

    mdh wrote:
    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.
    The loop stops when `s' points at '\0' *before* being
    incremented, but the increment happens regardless of the
    outcome of the test. After the loop, `s' points not at
    the '\0', but at the character immediately after it.

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • mdh

      #3
      Re: trying to find the error

      On May 1, 8:22 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
      , but the increment happens regardless of the
      outcome of the test.
      Ok...I understand. Thank you.

      Comment

      • mdh

        #4
        Re: trying to find the error

             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

        Comment

        • Ian Collins

          #5
          Re: trying to find the error

          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.

          Comment

          • mdh

            #6
            Re: trying to find the error

            On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            >
            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.

            Comment

            • jt

              #7
              Re: trying to find the error

              On May 2, 9:39 am, mdh <m...@comcast.n etwrote:
              On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.co mwrote:
              >
              >
              >
              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.sin ce
              here its post incrementation, s is incremented in the next statement.
              probably you can overcome this by using

              while(*s)s++;

              Comment

              • Ian Collins

                #8
                Re: trying to find the error

                jt wrote:
                On May 2, 9:39 am, mdh <m...@comcast.n etwrote:
                >On May 1, 9:20 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                >>
                >>
                >>
                >>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.sin ce
                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.

                Comment

                • rio

                  #9
                  Re: trying to find the error


                  "mdh" <mdeh@comcast.n etha scritto nel messaggio
                  news:75bdeebe-38ca-4219-9dab-c335a122aa41@b5 g2000pri.google groups.com...
                  >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 */
                  if s==0 there is an error
                  if *s=='\0' there is an error

                  is it better? "if(s) {while(*s) ++s;}
                  else return 0;
                  "
                  /* stops at '\0' */ /* <<<<<2 */
                  while ( *t && n-- 0)
                  *s++ = *t++; /* /*<<<<< 3 */
                  why do you want the below and not a simple
                  "if(n-->0) *s=0;
                  else return error;"
                  while ( n -- 0);
                  *s++ = '\0';
                  >
                  }
                  >
                  Now, with 1 I **Thought** that *s++ fails when *s == '\0', so that s
                  points to '\0'.
                  this suggest to me that "while ( *s++); " is not the right loop
                  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.


                  Comment

                  Working...