c语言一点小觉悟(1)
1 #include <stdio.h> 2 3 int a = 0x12345678; 4 int a2 = 0x12345678; 5 int a3 = 0x12345678; 6 int a4 = 0x12345678; 7 int main() 8 { 9 int b = 0x12345678; 10 int b2 = 0x12345678; 11 int b3 = 0x12345678; 12 int b4 = 0x12345678; 13 char *p1,*p2,*p3,p5; 14 int *p4,p6; 15 16 17 char *c = "hello"; 18 19 p1 = &b; 20 *p2 = &b; 21 p3 = &a; 22 p4 = &a; 23 *p4 = *p4+1; 24 p5 = a; 25 p6 = a; 26 printf("hello world\n"); 27 printf("int a addr is: %p\n",&a); 28 printf("int a2 addr is: %p\n",&a2); 29 printf("int a4 addr is: %p\n",&a3); 30 printf("int a3 addr is: %p\n",&a4); 31 printf("int b addr is: %p\n",&b); 32 printf("int b2 addr is: %p\n",&b2); 33 printf("int b3 addr is: %p\n",&b3); 34 printf("int b4 addr is: %p\n",&b4); 35 printf("int main addr is: %p\n",main); 36 printf("===========================\n"); 37 38 printf("pointer p1 addr is: %p\n",p1); 39 printf("*p1 addr is: %p\n",*p1); 40 printf("pointer p3 addr is: %p\n",p3); 41 printf("*p3 addr is: %p\n",*p3); 42 printf("pointer p4 addr is: %p\n",p4); 43 printf("*p4 addr is: %p\n",*p4); 44 printf("pointer p2 addr is: %p\n",p2); 45 printf("*p2 addr is: %p\n",*p2); 46 printf("===========================\n"); 47 printf("char p5 is : %p\n",p5); 48 printf("int p6 is:%p\n",p6); 49 50 printf("============================\n"); 51 printf("str addr is: %p\n","hello"); 52 printf("str addr is: %p\n",&"hello"); 53 printf("============================\n"); 54 printf("str addr is: %s\n",c); 55 printf("str addr is: %p\n",c); 56 printf("str addr is: %p\n",&c); 57 58 return 0; 59 } 60 /* 61 C:\Users\shuai\Desktop\c>gcc -o build 001.c & build 62 001.c: In function 'main': 63 001.c:14:5: warning: assignment from incompatible pointer type [-Wincompatible-p 64 ointer-types] 65 p1 = &b; 66 ^ 67 001.c:15:6: warning: assignment makes integer from pointer without a cast [-Wint 68 -conversion] 69 *p2 = &b; 70 ^ 71 001.c:16:5: warning: assignment from incompatible pointer type [-Wincompatible-p 72 ointer-types] 73 p3 = &a; 74 ^ 75 hello world 76 int a addr is: 0x402008 77 int a2 addr is: 0x40200c 78 int a4 addr is: 0x402010 79 int a3 addr is: 0x402014 80 int b addr is: 0x28cc44 81 int b2 addr is: 0x28cc40 82 int b3 addr is: 0x28cc3c 83 int b4 addr is: 0x28cc38 84 int main addr is: 0x4011d0 85 =========================== 86 pointer p1 addr is: 0x28cc44 87 *p1 addr is: 0x78 88 pointer p3 addr is: 0x402008 89 *p3 addr is: 0x79 90 pointer p4 addr is: 0x402008 91 *p4 addr is: 0x12345679 92 pointer p2 addr is: 0x612b5060 93 *p2 addr is: 0x44 94 =========================== 95 char p5 is : 0x79 96 int p6 is:0x12345679 97 ============================ 98 str addr is: 0x403060 99 str addr is: 0x403060 100 ============================ 101 str addr is: hello 102 str addr is: 0x403060 103 str addr is: 0x28cc40 104 105 106 */ 107 108 /* 109 结论: 110 1 系统中全局数据空间地址从小往大排列 111 系统中栈空间地址从栈顶往小排列 112 其中貌似main最先初始化,然后才有了后续事件 113 2 *p=*p+1 实际上是指针+1 等效于*p=*(p+1) 即对指针操作 114 *p=(*p)+1 指的是p地址上的数据+1 即对数据操作 注意两者不同 115 3 char *c="hello" ("%p\n",c)和("%p\n","hello")和("%p\n",&"hello")结果一样都是常量"hello"地址 116 ("%p\n",&c)取得是c的地址 117 ("%s\n",c)输出hello 118 4 *p=&b慎用,看起来不伦不类.并且结果未知,(*p==b?) 119 推荐使用p=&b,再*p即是b的值(*p==b为真) 120 121 122 */