string in C: confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elvira23
    New Member
    • Sep 2009
    • 1

    string in C: confusion

    Hi .

    According to a reference i am using, ANSI C does not have a built in string type. instead an array of characters need to be used. According to the book, the compiler ends then the array of char with the character zero '\o'. hence if you want to use a string of 4 chars, you need to declare an array of 5 chars.

    i wrote a simple code to verify the above
    Code:
    char m[6]="elvira";
    length=strlen(m); 
    for( i=0;i<length;i++)
    printf(" the chara at order %d os %c\n",i,m[i]);the output was to my suprise

    Code:
     the chara at order 0 os e
     the chara at order 1 os l
     the chara at order 2 os v
     the chara at order 3 os i
     the chara at order 4 os r
     the chara at order 5 os a
     the chara at order 6 os )
     the chara at order 7 os N
     the chara at order 8 os ├
    the above execution of the program was unexpected to me. if instead
    char m[]="elvira"; is used to declare and initiliase the string, printf outputs
    elvira ONLY!

    if instead i keep char m[6]="elvira" and then set m[5]='\0', the printf shows only "elvir" as expected. however, if i set m[i]='\0' for any i<6, only the characters up to (i-1) are displayed. The remaining are not displayed and "m" has a shorter length!!!



    i am surprised with such executions. i was expecting the array of char to be handled via its subscript like with any other type of array but it seems it is not the case

    why ??? they would have better define a string type. any explanations for the above please

    Cheers.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I'd say it was because you're declaring m as 6 indices long which, when the amount of characters put into it is 6, does not account for the string terminating character ('\0'). Make it m[7].

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      By the way, you answer your own question in your first sentence!

      "According to a reference i am using, ANSI C does not have a built in string type. instead an array of characters need to be used. According to the book, the compiler ends then the array of char with the character zero '\o'. hence if you want to use a string of 4 chars, you need to declare an array of 5 chars."

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You are confusing string length and array length.

        The str* functions work on strings, that is they look for the character '\0' and will happily go out of bounds in an array to find it.

        So when you wrote m[i]='\0' for any i<6 you altered the data and that altered the return value of strlen which operates on the data. If you had checked sizeof(m) you would have found it always to be the same unless you changed your declaration of m.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          FYI
          Originally posted by elvira23
          Code:
          char m[]="elvira";
          if instead i keep char m[6]="elvira" and then set m[5]='\0', the printf shows only "elvir" as expected. however, if i set m[i]='\0' for any i<6, only the characters up to (i-1) are displayed. The remaining are not displayed and "m" has a shorter length!!!
          By the way, by declaring m as an array there is no doubt -- you can overwrite portions of the string. However, if you had done this ...
          Code:
          char *p = "elvira";
          The compiler would have allocated a "hidden" char array, initialized it to "elvira", and then initialized p to point to that array. In this case, p points to a string literal; whether you could overwrite it (such as, p[5]='\0';) would be implementation dependent. Attempting to do so might have crashed your program!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Also keep in mind that C has no control on array bounds. That makes m[100] perfectly accessible even though the array was defined with only 6 elements.

            This is due to the fact that the array mane is the address of element 0. So
            m[100] is the address of m[0] + (100 x size of the element) and the compiler can accept this with no warning.

            It is your responsibility to control that array.

            Comment

            Working...