Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.