Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
 
Last edited:
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.
 
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
 
Last edited:
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
 
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.
 
Code:
char test[7] = {'s','c','h','o','o','l','\0'};

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 ];
 
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 ];

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
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.