为什么我得到一个警告试图通过一个'字符(* C)[6]“,以期待一个函数为const char(* C)[6]”? [英] Why do I get a warning trying to pass a 'char (*c)[6]' to a function expecting a 'const char (*c)[6]'?

查看:248
本文介绍了为什么我得到一个警告试图通过一个'字符(* C)[6]“,以期待一个函数为const char(* C)[6]”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关于的char ** VS 为const char ** 的东西(像的ç常见问题解答),但我看不到任何方案,其中有一个指针这样做将数组将导致有些内容本身实际上正在修改的阵列中。

I know about the char ** vs const char ** thing (like described in the c faq) but I can't see any scenario where doing so with a pointer to arrays would lead to some content inside the arrays themselves being actually modified.

我的code:

void fun(const char (*p)[6])
{
    printf("%s", p[0]);
}

int main(int argc, char *argv[])
{
    char a[6] = "hello";
    char (*c)[6];

    c = &a;

    fun(c);
}

在使用GCC编译给出了以下的输出:

gives the below output when compiled with gcc:

test.c:17:9: warning: passing argument 1 of 'fun' from incompatible pointer type
test.c:5:10: note: expected 'const char (*)[6]' but argument is of type 'char (*)[6]'

这里的问题 是某种联系,但一直无人接听为止。难道仅仅是编译器是偏执,唯一的办法摆脱的警告是显式转换?还是真的有机会可以的东西出问题?

The question here is somehow related but has no answer so far. Is it just the compiler being paranoïd and the only way to get rid of the warning is to explicitly cast ? Or is there really a chance something can go wrong ?

推荐答案

这仅仅是一个C语言规范的怪癖。再比如,在的char ** 为const char * const的* 转换也从常量,正确性,安全点看,但它禁止在C.

It is just a quirk of C language specification. For another example, the char ** to const char *const * conversion is also safe from the const-correctness point of view, yet it is prohibited in C.

的常量,正确性规则,这个怪癖是在C ++语言的固定,但C继续坚持其原有的规范在这方面。

This quirk of const-correctness rules was "fixed" in C++ language, but C continues to stick to its original specification in this regard.

这篇关于为什么我得到一个警告试图通过一个'字符(* C)[6]“,以期待一个函数为const char(* C)[6]”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆