Populating a char array.

Discussion in 'Mac Programming' started by larswik, Dec 22, 2010.

  1. larswik, Dec 22, 2010
    Last edited: Dec 22, 2010

    larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    When populating a char Array with a word like 'school', do you have to do it 1 character at a time with something like a FOR loop, or is there an easier way like
    Code:
    char word[6];
    *word = 'School';
    
    printf("The word is %s", word);
    
    
    I know the code is wrong but I used it as an example to show what I was thinking. I am learning that part of the book right now and I am trying my own tests to grasp it better.

    The book is teaching how to populate from a keyboard input. My question is what if I already have a word like 'school' and just want an char array from that.

    Thanks.

    -Lars
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    You can use strcpy() to do that, but for the example you have you need the array to be at least size 7 to fit 'school\0' or else it will be unterminated.

    Code:
    strcpy(word, "school");
    There is another version of strcpy() called strncpy() that copies at most n bytes from to the string to make sure you don't over run the space, but if you don't take the terminating zero into account even strncpy() will leave the string unterminated.
     
  3. lee1210, Dec 22, 2010
    Last edited: Dec 22, 2010

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Code:
    char test[7] = {'s','c','h','o','o','l','\0'};
    
    In your code the array is too short, no room for the terminator.

    String literals evaluate to char *, and you can't (as you've seen) assign a char * to an array. The strcpy will do it as well as the array initialization I showed above.

    -Lee
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    Ahhh, thanks guys. I have not learned the STRCPY yet in the book.

    Lee - I see what is happening with your line. That seems kind of obvious and I feel kinda stupid I did not see that.

    My brain needs extra memory.

    -Lars
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    For initialization you can also do like this and you will get the size for free, including the terminating zero.

    Code:
    char word[] = "school";
    Or alternatively like this:

    Code:
    const char *word = "school";
    The last one is a bit different though, it can't be changed, so it should be declared as const.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    This code produces the same result, and automatically sizes the array correctly:
    Code:
    char test[] = "school";
    
    The reason is that a C literal string is just as much a char-array as any other char-array. You can even subscript it:
    Code:
    int o = "school"[4];
    
    Here's a more realistic example:
    Code:
    char hexChar = "0123456789abcdef"[ someValue & 0x0f ];
    
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    Indeed, there are many ways to skin the proverbial cat. In my example the null terminator could have been left off and it would have been added implicitly due to array initialization rules (unspecified elements at the end will be zerod).

    I guess for a string literal this seems a bit odd. I'd think you'd normally have a pointer that you'd assign the pointer to the literal to. If you want an array that happens to start with a particular value (instead of being const), that might mean you'll want to change it later. For that reason I'd want to be able to see the size of the array explicitly. Nothing wrong with getting the size "for free" from an assignment, like I said: lots of ways to skin a cat.

    Knowing the types your literals evaluate to is quite important to. In this case it's helpful to know string literals evaluate to a char * leads to the ability to use the [] operator.

    Not disputing any points here, just adding some info. Also thought the general syntax for array initialization might be interesting.

    -Lee
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Thanks again. There always more then 1 way to do this which is nice. I am guessing that all of them are correct, some are just simpler then others but achieve the same result.

    -Lars
     

Share This Page