测试程序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void fun(int i); 5 6 int global_i = 100; 7 int global_j = 200; 8 int global_k,global_h; 9 char *global_p; 10 int main() 11 { 12 static int static_i = 5; 13 static int static_j = 10; 14 static int static_k; 15 static int static_h; 16 17 printf("\n全局数据地址(有初值):\n"); 18 printf("global_i\t 0x%p = %d\n", &global_i, global_i); 19 printf("global_j\t 0x%p = %d\n", &global_j, global_j); 20 printf("静态数据地址(有初值):\n"); 21 printf("static_i\t 0x%p = %d\n", &static_i, static_i); 22 printf("static_j\t 0x%p = %d\n", &static_j, static_j); 23 24 printf("\n全局数据地址(无初值):\n"); 25 printf("global_k\t 0x%p = %d\n", &global_k, global_k); 26 printf("global_h\t 0x%p = %d\n", &global_h, global_h); 27 printf("静态数据地址(无初值):\n"); 28 printf("static_k\t 0x%p = %d\n", &static_k, static_k); 29 printf("static_h\t 0x%p = %d\n", &static_h, static_h); 30 31 char *pstr1 = "Mr.Shao"; 32 char *pstr2 = "Hello"; 33 char *pstr3 = "Mr.Shao"; 34 printf("\n字符串常量数据地址:\n"); 35 printf("*pstr1\t 0x%p\n", pstr1); 36 printf("*pstr3\t 0x%p\n", pstr3); 37 printf("*pstr2\t 0x%p\n", pstr2); 38 39 int i = 5; 40 int j = 10; 41 int f, h; 42 char c='a'; 43 char s[] = "abc"; 44 char *p2=NULL; 45 char *p3 = "abc"; //"123456/0"在常量区,p3在栈上。 46 printf("\n栈中数据地址=有初值:\n"); 47 printf("i\t 0x%p = %d\n",&i,i); 48 printf("j\t 0x%p = %d\n", &j, j); 49 printf("f\t 0x%p = %d\n", &f, f); 50 printf("h\t 0x%p = %d\n", &h, h); 51 printf("c\t 0x%p = %d\n", &c, c); 52 printf("s\t 0x%p = 0x%p\n", &s, s); 53 printf("p2\t 0x%p = 0x%p\n", &p2, p2); 54 printf("p3\t 0x%p = 0x%p\n", &p3, p3); 55 56 const int NUM = 2; 57 int *p = (int*)malloc(NUM * sizeof(int)); 58 global_p = (char *)malloc(10); 59 p2 = (char *)malloc(20); 60 printf("NUM\t 0x%p = 0x%d\n", &NUM, NUM); 61 printf("p\t 0x%p = 0x%p\n", &p, p); 62 printf("\n堆中数据地址\n"); 63 printf("*p\t 0x%p\n", p); 64 printf("*global_p\t 0x%p\n", global_p); 65 printf("*p2\t 0x%p\n", p2); 66 67 68 printf("\n子函数的地址\n"); 69 printf("void fun(int)\t 0x%p\n", fun); 70 fun(47);//子函数 71 72 free(p); 73 free(global_p); 74 free(p2); 75 return 0; 76 } 77 78 void fun(int i) 79 { 80 int j = i; 81 static int static_i = 100; 82 static int static_j; 83 84 printf("\n子函数:\n"); 85 printf("栈中数据地址(参数)\n"); 86 printf("i\t 0x%p = %d\n", &i, i); 87 printf("栈中数据地址j\n"); 88 printf("j\t 0x%p = %d\n", &j, j); 89 printf("静态数据地址(有初值)\n"); 90 printf("static_i\t 0x%p = %d\n", &static_i, static_i); 91 printf("静态数据地址(无初值)\n"); 92 printf("static_j\t 0x%p = %d\n", &static_j, static_j); 93 }
输出:
实验总结