What is the problem with the code char *s = “hi”; s = ‘H’;?
Correct answer A. Modifying a string literal is undefined behaviour
Explanation
s points to the string literal itself, and attempting to modify a string literal is undefined behaviour; on Linux and macOS literals live in read-only memory, so the program usually crashes. It compiles because the literal has type char[3] in C, not const char. Declaring char s[] = "hi"; instead creates a modifiable array copy.
Answer checked How we write and check questions
Discussion