String literals vs const char* in C

前端 未结 5 1650
傲寒
傲寒 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:41

    gcc: Use the flag -Wwrite-strings

    PS. gcc manual explains why this isn't part of -Wall. Anyway, as always, you should find a combination of -W flags that suits your particular needs and coding style. For example, in a recent project I have used something like this: -Werror -Wall -Wextra -Wformat=2 -Winit-self -Wswitch-enum -Wstrict-aliasing=2 -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wdisabled-optimization -Wunused-macros -Wno-unused

提交回复
热议问题