strange c array

This is a discussion on strange c array within the C Programming forums, part of the General Programming Boards category; I wrote a small snippet of C code. Ignore the strange "sentence" name. My question is, why is it that ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    6

    strange c array

    I wrote a small snippet of C code. Ignore the strange "sentence" name. My question is, why is it that when I call strlen on this array, it prints 5, instead of 2? Thanks.

    Code:
    int main(){
    char sentence [2];
    sentence[0]= 'y';
    sentence[1]='o';
    int len= strlen(sentence);
    printf("%d", len);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Because you didn't leave room for the null terminator, leading to a buffer overflow.

    Code:
    int main(){
        char sentence [3];
        sentence[0]= 'y';
        sentence[1]= 'o';
        sentence[2]= 0;
        int len= strlen(sentence);
        printf("%d", len);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Strings end with a null byte. Your code needs to be changed to:
    Code:
    int main(void){
        char sentence [3];
        size_t len;
        sentence[0] = 'y';
        sentence[1] = 'o';
        sentence[2] = '\0';
        len= strlen(sentence);
        printf("%u", len);
        return 0;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Before fixing it, change your program to this to see what you get:

    Code:
    int main(){
    unsigned char sentence [2];
    printf("1: sen[0]=%01x, sen[1]=%01x, sen[2]=%01x\n", sentence[0], sentence[1], sentence[2]) ;
    sentence[0]= 'y';
    sentence[1]='o';
    printf("2: sen[0]=%02x, sen[1]=%02x, sen[2]=%02x\n", sentence[0], sentence[1], sentence[2]) ;
    int len= strlen(sentence);
    printf("%d", len);
    }
    You'll note that storage is not initialized, unless you specifically tell the compiler (or runtime) to initialize it.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    use %zu to print size_t, or %lu and cast to unsigned long in C89.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  2. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  3. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 07:42 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21