Char


char city[7] = "Dallas";
How many characters are stored in city?
Seven in total. Six for the letters in "Dallas" and one more for the terminating '\0' character at the end of the string. You could just say:

 
char city[] = "Dallas";

and the correct number of characters will be allocated to city.

On the other hand, you could say:

 
char city[100] = "Dallas";

and the string will still have a "length" (as determined by strlen) of six (strlen doesn't count the terminating '\0' character), but 100 characters will be allocated to the city array.
Last edited on
As as aside, we would be remiss if, having answered the question and provided some discussion, we did not also say just use a std::string. Stop using char arrays unless you really have to. You're not doing yourself any favours.
@Repeater, this is true, this is true.
Topic archived. No new replies allowed.