Basic introduction to strings
suggest changeA C string is a sequence of characters terminated by a null character, written '\0'. The null character is part of the stored representation but is not counted by strlen.
A string literal has array type and includes an implicit terminating null character:
const char *message = "hello world";
The characters of a string literal must not be modified. Although C permits assigning its address to char * for historical compatibility, attempting to modify the literal causes undefined behavior. Declaring the pointer const char * lets the compiler diagnose accidental modification through that pointer.
To create a modifiable string, initialize a character array:
char message[] = "hello world";
message[0] = 'H';
This array is equivalent to:
char message[] = {
'h', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd', '\0'
};
The array owns a copy of the literal's characters. Its size is inferred as 12 bytes in implementations where these source characters each occupy one byte.
Character arrays are not necessarily strings. An array is a valid string only if a null character occurs within its bounds. Passing an unterminated array to a function that expects a string can read beyond the array and cause undefined behavior.