In C, strings are implemented as arrays of characters, ending with a null byte ('\0'). Example of string declaration:
char str1[] = "hello"; // Array with 6 characters: {'h','e','l','l','o','\0'} char *str2 = "hello"; // Pointer to a string literal
const).To avoid errors:
malloc, strcpy, check buffer size).\0.Example of correct string handling:
char buffer[100]; strcpy(buffer, "test"); // OK, buffer is mutable and guaranteed to contain '\0'
What is the result of executing the following code and what errors will it lead to?
char *str = "hello"; str[0] = 'H'; printf("%s\n", str);
Answer: The program will lead to undefined behavior, most likely a segmentation fault, because string literals are placed in read-only memory. Writing values at the address of a string literal is not allowed.
Story The team confused the concepts of string array and pointer to literal. One function took
char *output = "default";and later executedstrcpy(output, input);, which resulted in a crash on the first run, because the copy was occurring in read-only memory.
Story When working with the network, the result was written to a buffer allocated with
char *buf = NULL; strcpy(buf, data);. This led to writing to uninitialized memory and crashing the application.
Story In a localization package, the team passed strings between components without ensuring that a
\0character was added. Once, the function printed garbage to the console and corrupted internal memory structure.