On Sun, 25 Apr 2021, Stefanos wrote:

       char *s = "Hello, world!";
   tmp.c:6: error: assignment discards qualifiers from pointer target type

With GCC and Clang does not throw any warning message.

(Aside: for me this is just a warning; are you compiling with -Werror?)

Without formal verification it's not possible to have static analysis that's 100% correct in all cases, so I'm not going to say this warning is 'correct', or that gcc and clang are 'incorrect' to emit it. However, I do think the warning is justified and will defend it.

String literals in c are not mutable.  In this snippet:

char *s = "hello world";
s[0] = 'H';

the behaviour of the assignment on the second line is undefined. On most systems the string literal will be placed in a read-only section of memory and the write will cause a fault. It is not _incorrect_ to store the address of the string literal in a variable of type char*, nor is it incorrect to read from that variable, but it is _misleading_ because it implies that it would be correct to write to the values pointed thereby.

Due to a historical oversight (which happens to have been corrected in c++), string literals have type char[], not const char[]. Passing -Wwrite-strings makes them have const type; this is the same in gcc and tcc. The only difference is that -Wall enables -Wwrite-strings in tcc and not in gcc.

 -E

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to