内存地址解析(指针、指针的指针)

来源:互联网 发布:淘宝淘金币可以买吗 编辑:程序博客网 时间:2024/04/30 11:00
/*By Richard*/#include <stdio.h>int main(){short int i;char a;short int *pi;i=50;pi=&i;a='A';short int **ppi;ppi=πprintf("The address of i is\t:0x%08x\n",&i);printf("The address of a is\t:0x%08x\n",&a);printf("The address of pi is\t:0x%08x\n",&pi);printf("The address of ppi is\t:0x%08x\n",&ppi);printf("------------------------------\n");printf("The value of i is\t:%d\n",i);printf("The value of a is\t:%c\n",a);printf("The value of pi is\t:0x%08x\n",pi);printf("The value of *pi is\t:%d\n",*pi);printf("The value of ppi is\t:0x%08x\n",ppi);printf("The value of *ppi is\t:0x%08x\n",*ppi);printf("The value of **ppi is\t:%d\n",**ppi);/***********************************************一、内存组成:*1,栈区stack--参数值、局部变量--编译器自动分配*2,堆区heap--由程序员分配释放--malloc、new*3,全局区(静态区)static--全局变量、静态变量--系统释放*4,文字常量区--系统释放*5,程序代码区--函数的二进制代码*我们这里的程序没有全局变量,main里面定义的变量都分配在栈区*二、栈区的地址分布:*【低端内存地址】*    ..............*【高端内存地址】*三、分析此程序(下面为各变量的地址):*ppi:0x0012FF3C(栈顶--低地址)*pi: 0x0012FF48*a:  0x0012FF57*i:  0x0012FF60(栈底--高地址)*四、实际分配:*(指针都分配四个字节,char都是一个字节,short int为二个字节)*ppi:0x0012FF3C 0x0012FF3D 0x0012FF3E 0x0012FF3F.....(ppi分配四个字节,其余字节为填充字节,为cc)*       48         ff         12         00(显然,存放的为pi的地址)*pi :0x0012FF48 0x0012FF49 0x0012FF4A 0x0012FF4B......(pi分配四个字节,其余字节为填充字节)*       60         ff         12         00(显然,存放的为i的地址)*a  :0x0012FF57......(a分配一个字节,其余字节为填充字节)*       41(41的十进制为65,对应的即为‘A’)*i  :0x0012FF60 0x0012FF61......(i分配两个字节,其余字节为填充字节)*          32          00(32的十进制为65)*五、程序结果*The address of i is    :0x0012ff60*The address of a is    :0x0012ff57*The address of pi is   :0x0012ff48(pi指针自己的内存地址)*The address of ppi is  :0x0012ff3c(ppi指针自己的内存地址)*----------------------------------*The value of i is :50*The value of ais :A*The value of piis :0x0012ff60(指向i,所以自身的值即为i的内存地址)*The value of *pi is  :50(*pi为i的值)*The value of ppi is  :0x0012ff48(指向pi,所以自身的值为pi的内存地址)*The value of *ppi is :0x0012ff60(*ppi为pi的值,而pi的值为i的内存地址)*The value of **ppi is:50(**pi为*pi的值,即为i)***********************************************/return 0;}