August Karlstrom
August Karlstrom

Reputation: 11377

Treating string literals as constants

Is there a way to make GCC issue a warning when a string literal is assigned to a non-constant character pointer?

Example:

const char *source;
char *target;

target = source;
target = "hello";

GCC warns about assigning the constant pointer source to the non-constant pointer target but the string literal assignment is unfortunately accepted.

Upvotes: 2

Views: 68

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272762

Use -Wwrite-strings. From the GCC manual:

-Wwrite-strings

When compiling C, give string constants the type const char[length] so that copying the address of one into a non-const char * pointer produces a warning.

Upvotes: 4

Related Questions