Why doesn't the compiler detect and create errors when trying to modify char * string literals?

Assume the following two pieces of code:

char *c = "hello world";
c[1] = 'y';

The above does not work.

char c[] = "hello world";
c[1] = 'y';

It does.

Regarding the first, I understand that the string "hello world" can be stored in a read-only memory section and therefore cannot be changed. The second, however, creates an array of characters on the stack and, therefore, can be changed.

My question is this: why don't compilers detect the first type of errors? Why not this part of the C standard? Is there any special reason for this?

+4
source share
4 answers

C compilers are not required to detect the first error, because C string literals are not const.

N1256 C99:

6.4.5 5:

7 , . , . char ; [...]

6:

, , . , undefined.

(C11 .)

, "hello, world" char[13] (not const char[13]), char* .

const undefined, , , (, ). undefined, , const ( ); , , undefined.

, :

#include <stdio.h>

void print_string(char *s) {
    printf("%s\n", s);
}

int main(void) {
    print_string("Hello, world");
    return 0;
}

const, "hello, world" , ( const) char*, . , undefined, print_string() , s.

. pre-ANSI C const, , char* promises, , . const ANSI C (1989) , .

gcc -Wwrite-strings , const, gcc , :

const char (*p)[6] = &"hello";

("hello" char[6], &"hello" char (*)[6], p. -Wwrite-strings, &"hello" type const char (*)[6].) , -Wall, -Wextra -Wwrite-strings.

, , -Wwrite-strings, , . C-, , -Wwrite-strings.

( , ++ const, , Bjarne Stroustrup ++, C.)

+4

"".

gcc, -Wwrite-strings, , const char* char*. ++.

, - , c[1] = 'y'. , char*, .

man 1 gcc:

When compiling C, give string constants the type "const char[length]" so that
copying the address of one into a non-"const" "char *" pointer will get a warning.
These warnings will help you find at compile time code that can try to write into a
string constant, but only if you have been very careful about using "const" in
declarations and prototypes. Otherwise, it will just be a nuisance. This is why we
did not make -Wall request these warnings.

, , const- C, gcc. g++.

+3

-Wwrite-strings , , . , , -Wall.

% cat chars.c 
#include <stdio.h>

int main()
{
  char *c = "hello world";
  c[1] = 'y';
  return 0;
}
% gcc -Wall -o chars chars.c          
% gcc -Wwrite-strings -o chars chars.c
chars.c: In function ‘main’:
chars.c:5: warning: initialization discards qualifiers from pointer target type

:

C "const char [length]", non "const" "char *" . , , "const" . . -Wall .

++ "char *". ++.

, " ++", , ( ) , -Wall . , -Wall.

, C99, 6.4.5 6 (. 63 PDF) :

, , . , .

+2

char* c = strdup("..."); c[1] . ( rant C). / , C , (//...) "" .

lintis a tool to detect such errors: a has const char*been assigned char*. He would also mark a label char c = c[30];(no longer dependent on type, as well as an addressing error.) How it would be nice to declare c as const char*. C is an older language with a tradition of indulgence and works on many platforms.

-1
source

All Articles