String literals vs const char* in C

前端 未结 5 1639
傲寒
傲寒 2020-12-09 20:25

Why don\'t ANSI C compilers flag the use of a string literal argument in a function call in which the correponding parameter does not have a const qualifier? For example,

5条回答
  •  感情败类
    2020-12-09 20:42

    The GNU compiler (and the Intel C compiler as well, iirc) will emit a warning, if -Wwrite-string is used:

    $ gcc -Wall -Wwrite-strings -o foo /tmp/foo.c
    /tmp/foo.c: In function 'main':
    /tmp/foo.c:12: warning: passing argument 1 of 'somefunc' discards qualifiers from pointer target type
    /tmp/foo.c:3: note: expected 'char *' but argument is of type 'const char *'
    

    Concerning VS2010, I can't help you.

提交回复
热议问题