简体   繁体   English

当初始化不带字符串的const数组时,为什么gcc不会发出警告?

[英]Why gcc does not give a warning when you initialize an array without const with strings?

#include <stdio.h>

void print(char *strings[]) {
  while (*strings) printf("%s\n", *strings++);
}

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

  char *array[] = {"Hello", "World", NULL}; // No warning?
  const char *constArray[] = {"Hello", "World", NULL};

  print(constArray); // Warning!

  //constArray[0][0] = '!'; Compile time error
  array[0][0] = '!'; // Run time error

  return 0;
}

I was expecting to get a warning in char *array[] = {"Hello", "World", NULL}; 我期望在char *array[] = {"Hello", "World", NULL};得到警告char *array[] = {"Hello", "World", NULL}; because the characters of those strings are read only, but the compiler does not give me a warning about that. 因为这些字符串的字符是只读的,但是编译器没有为此警告我。 So basically the compiler is letting me "cast" a const char to a char a without warning. 因此,基本上,编译器让我在没有警告的情况下将const char转换为char a。

When passing a const char to a function that receives a char in print(constArray); 当通过一个const char到其接收的功能charprint(constArray); , in other words, "casting" a const char , to a char the compiler does give me a warning. ,换句话说,将const char “ cast”到char编译器确实会警告我。 I was expecting the compiler to give me a warning in both cases or in neither, but not in one case and not the other. 我期望编译器在两种情况下都不会给我警告,但在任何一种情况下都不会给我警告,而在另一种情况下则不会给我警告。

I think this warning is important to help prevent errors like in array[0][0] = '!'; 我认为此警告很重要,有助于防止出现array[0][0] = '!'; . So why I don't get a warning in the first initialization? 那么,为什么在第一次初始化时没有收到警告?

So why I don't get a warning in the first initialization? 那么,为什么在第一次初始化时没有收到警告?

Because the type of a string literal is array of char , not array of const char , notwithstanding the fact that modifying the elements of such an array produces undefined behavior. 由于字符串文字的类型是char数组,而不是const char数组,尽管修改此类数组的元素会产生未定义的行为。 This comes down from the very first days of C, when there was no const . 这是从C的第一天开始的,当时没有const I'm sure its persistence into modern C revolves around the magnitude and scope of the incompatibility that would arise if the type were changed. 我确信它对现代C语言的持久性围绕着类型更改会引起的不兼容的程度和范围。

With respect to individual programs, however, GCC can help you out. 但是,对于个别程序,GCC可以为您提供帮助。 If you turn on its -Wwrite-strings option then it will indeed give string literals type const char [ length ] , with the result that a construct such as you presented will elicit a warning. 如果您打开它的-Wwrite-strings选项,那么它的确会给出类型为const char [ length ]字符串文字,其结果是,您所提出的结构将引发警告。

暂无
暂无

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

相关问题 为什么数组中的char在gcc中为newlib中的isspace给出下标警告 - Why is does char from an array give subscript warning in gcc for isspace in newlib 为什么GCC在将无符号长度设置为2 ^ 64-1时会发出警告? - Why does GCC give a warning when setting an unsigned long to 2^64-1? 在fopen()中使用无效模式时,为什么gcc不会给出警告或错误? - Why gcc does not give a warning or error when invalid mode is used in fopen()? 为什么GCC会向我发出此-Wdiscarded-qualifiers警告? - why does GCC give me this `-Wdiscarded-qualifiers` warning? 为什么gcc不会在代码里面的未定义行为中发出警告? - Why does gcc not give a warning at undefined behaviour in code inside? 为什么GCC会给我-Wjump-misses-init警告? - Why does GCC give me the -Wjump-misses-init warning? 为什么 gcc 给出警告说常量是无符号的,因为它太大了,而它是 __int128 类型并且是有符号的? - Why does gcc give a warning saying that the constant is unsigned because it is too large, when it is of the type __int128 and is signed? gcc是否将长字符串初始化为“”“但不是短字符串? - Does gcc initialize long strings to `“”` but not short ones? 为什么溢出数组初始化会发出警告但溢出的赋值不会? - Why does overflowing array initialization give warning but overflowing assignment does not? 为什么当我转换为float时gcc总是会出错? - Why does gcc always give a error when i convert to float?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM