Char

Aug 4, 2018 at 3:57am

char city[7] = "Dallas";
How many characters are stored in city?
Aug 4, 2018 at 4:14am
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 Aug 4, 2018 at 4:15am
Aug 4, 2018 at 9:00am
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.
Aug 4, 2018 at 4:44pm
@Repeater, this is true, this is true.
Topic archived. No new replies allowed.