Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中带有常量签名的错误调用_C_Warnings_Constants - Fatal编程技术网

C中带有常量签名的错误调用

C中带有常量签名的错误调用,c,warnings,constants,C,Warnings,Constants,可能重复: 为什么我会在下面调用foo时收到警告(gcc 42.2) void foo(const char **str) { (*str)++; } (...) char **str; foo(str); (...) 我理解为什么我们不能调用一个函数,除了一个char**和一个const char**,但我认为相反的情况是可以的,那么为什么会出现以下警告呢 warning: passing argument 1 of 'foo' from incompatible pointer

可能重复:

为什么我会在下面调用foo时收到警告(gcc 42.2)

void foo(const char **str)
{
  (*str)++;  
}

(...)
char **str;
foo(str);
(...)
我理解为什么我们不能调用一个函数,除了一个
char**
和一个
const char**
,但我认为相反的情况是可以的,那么为什么会出现以下警告呢

warning: passing argument 1 of 'foo' from incompatible pointer type

这是错误的。这里没有与编译器争论的真正空间,因为它受到规范的支持。下面是一个例子,它确切地解释了为什么它是错误的:

void func(const char **p)
{
    *p = "abc";
}

void func2(void)
{
    char *a;
    func(&a);
    *a = 'x';
}
如果编译器没有抛出错误,您的程序可能会崩溃,因为您将覆盖字符串文字(通常在内存中标记为只读)

因此,您不能将
char**
隐式强制转换为
const char**
,因为这将允许您从任何值中删除
const
限定符-基本上,它将允许您在没有显式强制转换的情况下随意忽略
const


编译器给出警告而不是错误的主要原因是,许多旧代码没有使用
const
限定符,如果代码是今天编写的话。

您使用的是哪些编译标记?我认为这是不合法的,尽管有了这样一个不兼容的赋值,您在某些情况下可以不使用subversit-const铸造