Allocate space for char

c++

Solution

For the special `'\0'` char which indicates end of string.

(Remember, C-style strings are null-terminated arrays).

Additional helpful notes:

- `strlen` does not count the `'\0'` (That's why you need this extra byte).

- `strcpy` does copy the `'\0'`.

- `char str[7] = "String";` - Adds `'\0'` by itself.

- `char str[] = {'S','t','r','i','n','g'}` - Does not add `'\0'`.

- `char str[7] = {'S','t','r','i','n','g'}` - Will add `'\0'`.

Problem

String construct got space for the data by doing ``` new char[strlen(cp)+1]; ``` Since there are only `strlen(cp)`characters in the string, what is the extra byte for?

Original source