why

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

    why

    I am using suse 9.3 pro
    Why is it when i run this program in the konsole

    i get the out put:
    1st string: To be or not to be?
    2nd string: To bTo be or not to be?

    which it should not be

    but when i run the same program in the ddd debugger i get
    1st string: to be or not to be:
    2nd string: to be

    which it should be according to the book: c programming in easy steps
    the program is below:

    #include<stdio. h>
    #include<string .h> /* make strncpy available */

    int main(void)
    {
    char string1[] = "To be or not to be?";
    char string2[6];

    /* copy first 5 characters in string1 to string2 */
    strncpy(string2 , string1, 5);

    printf("1st string: %s\n", string1);
    printf("2nd string: %s\n", string2);
    return 0;
    }

  • Robert Gamble

    #2
    Re: why

    Darklight wrote:[color=blue]
    > I am using suse 9.3 pro
    > Why is it when i run this program in the konsole
    >
    > i get the out put:
    > 1st string: To be or not to be?
    > 2nd string: To bTo be or not to be?
    >
    > which it should not be
    >
    > but when i run the same program in the ddd debugger i get
    > 1st string: to be or not to be:
    > 2nd string: to be
    >
    > which it should be according to the book: c programming in easy steps
    > the program is below:
    >
    > #include<stdio. h>
    > #include<string .h> /* make strncpy available */
    >
    > int main(void)
    > {
    > char string1[] = "To be or not to be?";
    > char string2[6];
    >
    > /* copy first 5 characters in string1 to string2 */
    > strncpy(string2 , string1, 5);
    >
    > printf("1st string: %s\n", string1);
    > printf("2nd string: %s\n", string2);
    > return 0;
    > }[/color]

    Despite its name, strncpy does not guarantee that the destination will
    have a null terminator (and thus be a string). In your case, since
    there is no null character in the first 5 characters of the source
    string, 5 non-null characters will be stored in string2, the last
    character is indeterminate. When you printf string2, undefined
    behavior occurs because it is not a string. If you initialized string2
    like this:
    char string2[6] = {0}
    you would get the behavior you expect.

    Robert Gamble

    Comment

    • Skarmander

      #3
      Re: why

      Darklight wrote:[color=blue]
      > I am using suse 9.3 pro
      > Why is it when i run this program in the konsole
      >
      > i get the out put:
      > 1st string: To be or not to be?
      > 2nd string: To bTo be or not to be?
      >
      > which it should not be
      >
      > but when i run the same program in the ddd debugger i get
      > 1st string: to be or not to be:
      > 2nd string: to be
      >
      > which it should be according to the book: c programming in easy steps
      > the program is below:
      >
      > #include<stdio. h>
      > #include<string .h> /* make strncpy available */
      >
      > int main(void)
      > {
      > char string1[] = "To be or not to be?";
      > char string2[6];
      >
      > /* copy first 5 characters in string1 to string2 */
      > strncpy(string2 , string1, 5);
      >[/color]
      <snip>

      Strings in C are null-terminated. strncpy copies only the first five
      bytes of the string, which does not include a terminating null.
      Therefore the string is not well-defined. You need either

      string2[5] = '\0';

      after the strncpy or

      char string2[6] = {'\0'};

      to initialize string2 with all NULs.

      The reason you do not see this problem when debugging is because the
      contents of uninitialized memory apparently include a NUL character, and
      it happens to work. In general, anything could happen, including a
      program crash.

      S.

      Comment

      • Skarmander

        #4
        Re: why

        Skarmander wrote:[color=blue]
        > Darklight wrote:
        >[color=green]
        >> I am using suse 9.3 pro
        >> Why is it when i run this program in the konsole
        >>
        >> i get the out put:
        >> 1st string: To be or not to be?
        >> 2nd string: To bTo be or not to be?
        >>
        >> which it should not be
        >>
        >> but when i run the same program in the ddd debugger i get
        >> 1st string: to be or not to be:
        >> 2nd string: to be
        >>
        >> which it should be according to the book: c programming in easy steps
        >> the program is below:
        >>
        >> #include<stdio. h>
        >> #include<string .h> /* make strncpy available */
        >>
        >> int main(void)
        >> {
        >> char string1[] = "To be or not to be?";
        >> char string2[6];
        >>
        >> /* copy first 5 characters in string1 to string2 */
        >> strncpy(string2 , string1, 5);
        >>[/color]
        > <snip>
        >
        > Strings in C are null-terminated. strncpy copies only the first five
        > bytes of the string, which does not include a terminating null.[/color]

        Uh oh, overloading alert. Make this "NUL-terminated" and "terminatin g
        NUL", to avoid confusion with null pointers and NULL.

        S.

        Comment

        • pete

          #5
          Re: why

          Darklight wrote:[color=blue]
          >
          > I am using suse 9.3 pro
          > Why is it when i run this program in the konsole
          >
          > i get the out put:
          > 1st string: To be or not to be?
          > 2nd string: To bTo be or not to be?
          >
          > which it should not be
          >
          > but when i run the same program in the ddd debugger i get
          > 1st string: to be or not to be:
          > 2nd string: to be
          >
          > which it should be according to the book: c programming in easy steps
          > the program is below:[/color]

          Really? That's some bad code.
          It doesn't put a null character in string2,
          and there's no such thing a string without a null character, in C.

          [color=blue]
          >
          > #include<stdio. h>
          > #include<string .h> /* make strncpy available */
          >
          > int main(void)
          > {
          > char string1[] = "To be or not to be?";
          > char string2[6];[/color]

          If it were

          char string2[6] = "";

          then that would be OK
          [color=blue]
          >
          > /* copy first 5 characters in string1 to string2 */
          > strncpy(string2 , string1, 5);[/color]


          string2[5] = '\0';

          /*
          ** If string2 is not initialized with null characters
          ** then you have to put one in there at the end.
          */
          [color=blue]
          > printf("1st string: %s\n", string1);
          > printf("2nd string: %s\n", string2);
          > return 0;
          > }[/color]


          --
          pete

          Comment

          • Darklight

            #6
            Re: why

            Thanks for your help, but this is what it says in the book

            The example below copies a string of five characters into an array
            of six elements-so string2[5] receives a \0 null character.

            Comment

            • Skarmander

              #7
              Re: why

              Darklight wrote:[color=blue]
              > Thanks for your help, but this is what it says in the book
              >
              > The example below copies a string of five characters into an array
              > of six elements-so string2[5] receives a \0 null character.
              >[/color]
              This goes to show you that you can't believe everything you read.

              This is a pretty explicit mistake; I'd look around for another book.

              According to the ACCU, this book is "Not Recommended".


              S.

              Comment

              • pete

                #8
                Re: why

                Skarmander wrote:[color=blue]
                >
                > Darklight wrote:[color=green]
                > > Thanks for your help, but this is what it says in the book
                > >
                > > The example below copies a string of five characters into an array
                > > of six elements-so string2[5] receives a \0 null character..[/color][/color]

                I'm not a proponent of book burning per se,
                but if you have fireplace and it gets cold this Winter ...
                [color=blue]
                > This goes to show you that you can't believe everything you read.
                >
                > This is a pretty explicit mistake; I'd look around for another book.
                >
                > According to the ACCU, this book is "Not Recommended".
                > http://www.accu.org/bookreviews/publ.../c/c003566.htm
                >
                > S.[/color]

                --
                pete

                Comment

                Working...