指针需要倒着理解为"pointer to"
-
int *a ==> 指向int的指针,pointer to int -
const int *b ==> 指向const int的指针,pointer to const int,这个叫指针常量,对应的指向数值不能变,指向的数值是常量,const在_前
- int * const c ==>指向int的不可变指针,const pointer to int ,这个叫常量指针,指针自身的地址不能改变,指针自己是常量,const在*后
例子
int 四字节 short 两字节 char 一字节
int num = 0x01020304;
int *p1 = #
short *p2;
char *p3;
printf("p1 = %x\n",*p1);//p1 = 0x1020304(第一位的0省略了)
p2 = (short *)p1;
printf("p2 = %x\n",*p2);//p2 = 0x304
p3 = (char *)p1;
printf("p3 = %x\n",*p3);//p3 = 0x4
return 0;
200对应的二级制是1100 1000b (有效长度一个字节) 使用short的两字节也可以覆盖,但是char型只有一字节,最高位是1则认为是负数,即
int num = 200;
int *p1 = #
short *p2;
char *p3;
printf("p1 = %d\n",*p1);//p1 = 200
p2 = (short *)p1;
printf("p2 = %d\n",*p2);//p2 = 200
p3 = (char *)p1;
printf("p3 = %d\n",*p3);//p3 = -56
return 0;
|