当我编译这样的事情
双达[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:wwwpileonline/compile_c_online.php
2)MSVC(如C ++ code)版本VS2012CTP适用于x86 17.00.51025: rise4fun/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: okgcc 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: okwhile 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: errorgcc gives the same warning but MSVC gives error(!).
Who is right here? gcc or MSVC?
Test results.
Compilers:
1) gcc version 4.7.2: wwwpileonline/compile_c_online.php
2) MSVC (as C++ code) version 'VS2012CTP' 17.00.51025 for x86: rise4fun/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 hereBasically 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 typeBut 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;指针数组计算值:不相容指针
发布评论