分配与所述;指针常量的制造&gt阵列; = LT;指针数组计算值:不相容指针 [英] Assignment <pointer to array of constants> = <pointer to array>: incompatible pointers

查看:163
本文介绍了分配与所述;指针常量的制造&gt阵列; = LT;指针数组计算值:不相容指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译这样的事情

 双达[3] = {2,3,4};
双(* PDA)[3] =&放大器; DA;
双常量(* CPDA)[3] = PDA; // GCC:警告; MSVC:确定

GCC警告我

 警告:初始化从兼容的指针类型[默认启用]

问题:什么是这个任务的问题?是的,从技术上说,这是不同的类型,但我没看到任何危险,双常量(*)[3] 看起来更安全,我不是双(*)[3]

我做了一些测试,结果混淆我更:

1)MSVC与双常量(* CPDA)[3] = PDA很高兴。分配,没有错误,没有警告

2),GCC和MSVC很高兴与此

 双D = 1;
双* PD =和D;
双常量* CPD = PD; // GCC:OK; MSVC:确定

,而这些不同类型的太

3)在本实施例

 双D = 1;
双* PD =和D;
双* PPD =安培; PD;
双常量* CPPD = PPD; // GCC:警告; MSVC:错误

GCC给出了同样的警告,但MSVC给出错误(!)。

是谁在这里? GCC或MSVC?


测试结果。

编译器:

1)gcc版本4.7.2:<一href=\"http://www.compileonline.com/compile_c_online.php\">http://www.compileonline.com/compile_c_online.php

2)MSVC(如C ++ code)版本VS2012CTP适用于x86 17.00.51025: http://rise4fun.com/vcpp

3)MSVC(如C code)VS2010:离线测试

  INT的main()
{
    双D = 1;    双* PD =和D;
    双常量* CPD = PD;
    // GCC:确定
    // MSVC C ++:OK
    // MSVC C:OK    双* PPD =安培; PD;
    双常量* CPPD = PPD;
    // GCC:警告:默认情况下启用]从兼容的指针类型初始化
    // MSVC C ++:错误C2440:初始化:不能从双**转换为const的双**
    // MSVC C:OK    双哒[3] = {2,3,4};    双(* PDA)[3] =&放大器; DA;
    双常量(* CPDA)[3] = PDA;
    // GCC:警告:默认情况下启用]从兼容的指针类型初始化
    // MSVC C ++:OK
    // MSVC C:OK    CPD,CPDA;
    返回0;
}


编辑:

我刚刚编译这对我的Visual Studio为C code(不是C ++),这让没有错误,没有警告的。我编辑评论上面code


解决方案

GCC 就在这里和诊断在C必需的。

 双达[3] = {2,3,4};
双(* PDA)[3] =&放大器; DA;
双常量(* CPDA)[3] = PDA; //这里诊断

基本上你正在尝试键入 T1 的对象赋给类型的对象 T2 (简单的限制转让申请初始化)。

其中, T1 是一个指向 T的数组 N

T2 是一个指向常量T的数组 N

在简单赋值的制约,C说,对指针下应成立(C99,6.5.16.1p1):


  

两个操作数为指针类型兼容合格的或不合格的版本,类型由左指向具有类型的所有预选赛的权

指出,

这将允许例如是这样的:

  int类型的= 0;
const int的* p =&放大器;一个; // p型是&放一个合格的版本;一个类型

但是,在你的榜样,一个指向常量牛逼阵列 N 就不是一个合格的版本指针 T 阵列 N 。在C数组不能常数:没有常量阵列,但常量元素的唯一阵列。

When I compile something like this

double da[ 3 ] = { 2., 3., 4. };
double (* pda)[ 3 ] = &da;
double const (* cpda)[ 3 ] = pda; // gcc: warning; MSVC: ok

gcc warns me

warning: initialization from incompatible pointer type [enabled by default]

Question: What's the problem with this assignment? Yes, technically, these are different types, but I don't see any danger here, double const (*)[ 3 ] looks even safer for me than double (*)[ 3 ].

I did some tests and results confuse me even more:

1) MSVC is quite happy with double const (* cpda)[ 3 ] = pda; assignment, no errors, no warnings.

2) Both gcc and MSVC are happy with this

double d = 1.;
double * pd = &d;
double const * cpd = pd;  // gcc: ok; MSVC: ok

while these are different types too.

3) In this example

double d = 1.;
double * pd = &d;
double * * ppd = &pd;
double const * * cppd = ppd;  // gcc: warning; MSVC: error

gcc gives the same warning but MSVC gives error(!).

Who is right here? gcc or MSVC?


Test results.

Compilers:

1) gcc version 4.7.2: http://www.compileonline.com/compile_c_online.php

2) MSVC (as C++ code) version 'VS2012CTP' 17.00.51025 for x86: http://rise4fun.com/vcpp

3) MSVC (as C code) VS2010: tested offline

int main()
{
    double d = 1.;

    double * pd = &d;
    double const * cpd = pd;
    // gcc: ok
    // MSVC C++: ok
    // MSVC C: ok

    double * * ppd = &pd;
    double const * * cppd = ppd;
    // gcc: warning: initialization from incompatible pointer type [enabled by default]
    // MSVC C++: error C2440: 'initializing' : cannot convert from 'double **' to 'const double **'
    // MSVC C: ok

    double da[ 3 ] = { 2., 3., 4. };

    double (* pda)[ 3 ] = &da;
    double const (* cpda)[ 3 ] = pda;
    // gcc: warning: initialization from incompatible pointer type [enabled by default]
    // MSVC C++: ok
    // MSVC C: ok

    cpd, cpda;
    return 0;
}


Edit:

I just compiled this on my Visual Studio as C code (not C++) and it gives no errors, no warnings at all. I edited commentaries to above code

解决方案

gcc is right here and the diagnostic is required in C.

double da[ 3 ] = { 2., 3., 4. };
double (* pda)[ 3 ] = &da;
double const (* cpda)[ 3 ] = pda;  // diagnostic here

Basically you are trying to assign an object of type T1 to an object of type T2 (constraints of simple assignment apply for initializations).

Where T1 is a pointer to an array N of T.

And T2 is a pointer to an array N of const T.

In the constraints of the simple assignment, C says that for pointers the following shall hold (in C99, 6.5.16.1p1):

both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right

This would allow for example something like:

int a = 0;
const int *p = &a;  // p type is a qualified version of &a type

But in your example, a pointer to an array N of const T is not a qualified version of a pointer to an array N of T. In C an array cannot be constant: there is not const arrays, but only arrays of const elements.

这篇关于分配与所述;指针常量的制造&gt阵列; = LT;指针数组计算值:不相容指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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