简体   繁体   English

字符串文字与C中的const char *

[英]String literals vs const char* in C

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? 为什么ANSI C编译器不会在函数调用中标记字符串文字参数的使用,其中相关参数没有const限定符? For example, the following code could generate an exception by trying to write to read only memory. 例如,以下代码可以通过尝试写入只读内存来生成异常。

void somefunc(char buffer[10]);

void somefunc(char buffer[10]) {
    int i;

    for (i = 0;   i < 10;   i++)
       buffer[i] = 0;
}

int main(int argc, char **argv) {

    somefunc("Literal");
    return 0;
}

This situation could be identified at compile time but VS2010 and gcc don't appear to do so. 这种情况可以在编译时识别,但VS2010和gcc似乎没有这样做。 Calling somefunc with a const char* argument will generate a compiler warning. 使用const char *参数调用somefunc将生成编译器警告。

It is a K&R legacy. 这是K&R的遗产。 Fixing it would break a million programs. 修复它将打破一百万个程序。

gcc: Use the flag -Wwrite-strings gcc:使用标志-Wwrite-strings

PS. PS。 gcc manual explains why this isn't part of -Wall. gcc手册解释了为什么这不是-Wall的一部分。 Anyway, as always, you should find a combination of -W flags that suits your particular needs and coding style. 无论如何,一如既往,您应该找到适合您特定需求和编码风格的-W标志组合。 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 例如,在最近的一个项目中我使用过这样的东西: -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

What Hans Passant said. 汉斯帕斯特说的话。 const was added as part of the ANSI standard on 1989, so anything from before that didn't have const. const是作为1989年ANSI标准的一部分添加的,因此之前的任何东西都没有const。

string literals are not const in C; 字符串文字在C中不是const; in C++ they are. 在C ++中他们是。

edit: to clear up any confusion about my comment, I am referring to the type, not the ability to actually change them. 编辑:清除对我的评论的任何困惑,我指的是类型,而不是实际更改它们的能力。

The GNU compiler (and the Intel C compiler as well, iirc) will emit a warning, if -Wwrite-string is used: 如果使用-Wwrite-string ,GNU编译器(以及Intel C编译器,iirc)将发出警告:

$ 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. 关于VS2010,我帮不了你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM