在linux下的gcc测试当类型中有多个*时,
最多两个const不会出现警告并且必须格式TYPE *const*const p=&a;
警告 const TYPE**p; TYPEconst**const p=&a;const TYPE **cosnt p=&a;TYPE *const*const*const*const*const..p;
且由后面两个警告得TYPEconst**const p=&a;和const TYPE **cosnt p=&a;等效。
只要不出错,警告出来的一般可以运行(但是是破坏性的)。
推测TYPE const*const*const*const*const*const p=&a;由const依次得到对应类型
TYPE、TYPE*、TYPE**、TYPE***、TYPE****、TYPE*****,显然对应的这些被限定为不可赋值.当然一些const可能不会有。
因此我觉得TYPEconst*const p=&a;写成这样会好理解。
--------------------------以下全部在Linux下,使用的是gcc
正确(两个**两个const)
//编译 gcc const.c
//运行 ./a.out
#include<stdio.h>
#include<string.h>
typedef int TYPE;
int main()
{
TYPE A11=453;
TYPE A1=321;
TYPE *A2=&A1;
TYPE *A22=&A11;
TYPE *const*const p=&A2;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
A2=&A11;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
**p=A1;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
return 0;
}
[ pcdtest]$ ./a.out
memory**:0x7fffa4714d90 memory*:0x7fffa4714d98 data:321
memory**:0x7fffa4714d90 memory*:0x7fffa4714d9c data:453
memory**:0x7fffa4714d90 memory*:0x7fffa4714d9c data:321
正确(三个星两个const)
//编译 gcc const.c
//运行 ./a.out
#include<stdio.h>
#include<string.h>
typedef int TYPE;
int main()
{
TYPE A11=453;
TYPE A1=321;
TYPE *A2=&A1;
TYPE **A3=&A2;
TYPE **const*const p=&A3;
printf("memory***:%p memory**:%p memory:*:%p data:%d\n",p,*p,**p,***p);
A2=&A11;
printf("memory***:%p memory**:%p memory:*:%p data:%d\n",p,*p,**p,***p);
***p=A1;
printf("memory***:%p memory**:%p memory:*:%p data:%d\n",p,*p,**p,***p);
return 0;
}
**********************************以下两个警告,可以看到TYPE const**const p=&A2;和const TYPE**const p=&A2;等效均会造成警告。
const_.c: In function ‘main’:
const_.c:12: 警告:从不兼容的指针类型初始化
[ pcdtest]$ ./a.out
memory**:0x7fff0097c300 memory*:0x7fff0097c308 data:321
memory**:0x7fff0097c300 memory*:0x7fff0097c30c data:453
memory**:0x7fff0097c300 memory*:0x7fff0097c308 data:321
//编译 gcc const.c
//运行 ./a.out
#include<stdio.h>
#include<string.h>
typedef int TYPE;
int main()
{
TYPE A11=453;
TYPE A1=321;
TYPE *A2=&A1;
TYPE *A22=&A11;
TYPE const* *const p=&A2;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
A2=&A11;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
*p=&A1;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
return 0;
}
//编译 gcc const.c
//运行 ./a.out
#include<stdio.h>
#include<string.h>
typedef int TYPE;
int main()
{
TYPE A11=453;
TYPE A1=321;
TYPE *A2=&A1;
TYPE *A22=&A11;
const TYPE **const p=&A2;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
A2=&A11;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
*p=&A1;
printf("memory**:%p memory*:%p data:%d\n",p,*p,**p);
return 0;
}
*************************************
最后给个字符串的
警告:
const_.c: In function ‘main’:
const_.c:10: 警告:初始化丢弃了指针目标类型的限定
//编译 gcc const.c
//运行 ./a.out
#include<stdio.h>
#include<string.h>
typedef char* TYPE;
int main()
{
TYPE ca2="_before_me";
const TYPE ca="me";
TYPE *const p=&ca;
TYPE caf="afterme";
printf("memory:%p data:%s\n",&ca2,ca2);
printf("memory:%p data:%s\n",p,*p);
printf("memory:%p data:%s\n",&caf,caf);
//破坏性使用
*p="hello_word";
printf("memory:%p data:%s\n",&ca2,ca2);
printf("memory:%p data:%s\n",p,*p);
printf("memory:%p data:%s\n",&caf,caf);
printf("memory:%p data:%s\n",&ca,ca);
return 0;
}
打印结果:
memory:0x7fff719439d0 data:_before_me
memory:0x7fff719439c8 data:me
memory:0x7fff719439c0 data:afterme
memory:0x7fff719439d0 data:_before_me
memory:0x7fff719439c8 data:hello_word
memory:0x7fff719439c0 data:afterme
memory:0x7fff719439c8 data:hello_word