|
发表于 2021-5-17 15:42:41
|
显示全部楼层
- int main(void)
- {
- char *p1;
- short *p2;
- int *p3;
- long *p4;
- float *p5;
-
- char a[10];
- short b[10];
- int c[10];
- long d[10];
- float e[10];
-
-
- //第一步 看看类型在你机器上多大
- printf("type char lenth is %d\n\r",sizeof(char));
- printf("type short lenth is %d\n\r",sizeof(short));
- printf("type int lenth is %d\n\r",sizeof(int));
- printf("type long lenth is %d\n\r",sizeof(long));
- printf("type float lenth is %d\n\r",sizeof(float));
-
- //第二步 看看不同类型的指针大小是多少
- p1=&a[0];//p1=a;效果等价,但意义不同
- p2=&b[0];
- p3=&c[0];
- p4=&d[0];
- p5=&e[0];
- printf("type char* lenth is %d\n\r",sizeof(p1));
- printf("type short* lenth is %d\n\r",sizeof(p2));
- printf("type int* lenth is %d\n\r",sizeof(p3));
- printf("type long* lenth is %d\n\r",sizeof(p4));
- printf("type float* lenth is %d\n\r",sizeof(p5));
- //第三步 看看不同类型的指针内是什么
-
- printf("p1 vlue is %04x\t &a[0] address is %04x\n\r",(int)p1,(int)(&a[0]));
- printf("p2 vlue is %04x\t &b[0] address is %04x\n\r",(int)p1,(int)(&b[0]));
- printf("p3 vlue is %04x\t &c[0] address is %04x\n\r",(int)p1,(int)(&c[0]));
- printf("p4 vlue is %04x\t &d[0] address is %04x\n\r",(int)p1,(int)(&d[0]));
- printf("p5 vlue is %04x\t &e[0] address is %04x\n\r",(int)p1,(int)(&e[0]));
-
- //第四步 看看不同类型的指针++后有什么变化
-
- printf("p1 vlue is %04x\t",(int)p1);
- p1++;
- printf("p1 vlue is %04x\n\r",(int)p1);
-
- printf("p2 vlue is %04x\t",(int)p2);
- p1++;
- printf("p2 vlue is %04x\n\r",(int)p2);
- printf("p3 vlue is %04x\t",(int)p3);
- p1++;
- printf("p3 vlue is %04x\n\r",(int)p3);
-
- printf("p4 vlue is %04x\t",(int)p4);
- p1++;
- printf("p4 vlue is %04x\n\r",(int)p4);
-
- printf("p5 vlue is %04x\t",(int)p5);
- p1++;
- printf("p5 vlue is %04x\n\r",(int)p5);
-
- //第五步 灵活混搭一下看看
-
- a[0]=10;
- a[1]=11;
- a[2]=12;
- a[3]=13;
-
- b[0]=100;
- b[1]=110;
- b[2]=120;
- b[3]=130;
- p1=&a[0]; //p1 是char*类型的
-
- printf("*p1=%d\t",*p1);
- p1++;
- printf("*p1=%d\n\r",*p1);
- p1=(char*)&b[0];// 理解这步和一下的结果是本程序的核心
-
- printf("*p1=%x\t",*p1);//看看打印了些什么东西 100 = 0x0064
- p1++;
- printf("*p1=%x\n\r",*p1); //看看打印了些什么东西 100 = 0x0064
-
- p2=(short*)&a[0];
-
- printf("*p2=%x\t",*p1);//看看打印了些什么东西
- p1++;
- printf("*p2=%x\n\r",*p1); //看看打印了些什么东西
-
-
- }
复制代码
上面的程序看看 然后运行运行 然后就都明白了。 |
|