总结:
1.指针本身需要占用内存空间,指针的值是指向位置的地址(例如c) 2.数组名实际就是个地址。数组名的值是个指针常量,也就是数组第一个元素的地址,所以,数组名这个指针并不需要像普通指针c那样占用内存空间,它实际就是一个地址0xbffff3c8,在这个地址里存储的就是数组第一个元素。b=&b。
Output: &a is 0xbffff3cc a is 12 &b is 0xbffff3c8 b is 0xbffff3c8 *b is 12 &c is 0xbffff3c4 c is 0xbffff3c8
Breakpoint 1, main () at free_test.c:15 15 return 0; (gdb) x /4xb 0xbffff3cc 0xbffff3cc: 0x0c 0x00 0x00 0x00 (gdb) x /4xb 0xbffff3c8 0xbffff3c8: 0x0c 0x00 0x00 0x00 (gdb) x /4xb 0xbffff3c4 0xbffff3c4: 0xc8 0xf3 0xff 0xbf
分析:
注意: 1.指针的值是指向位置的地址(例如c) 2.数组名的值是个指针常量,也就是数组第一个元素的地址,所以,数组名这个指针并不需要像普通指针c那样占用内存空间,它实际就是一个地址0xbffff3c8,在这个地址里存储的就是数组第一个元素。b=&b。
程序2
Output: &a is 0xbffff3cc a is 12 &b is 0xbffff3c8 b is 0xbffff3cc *b is 12 &c is 0xbffff3c4 c is 0xbffff3c8
Breakpoint 1, main () at free_test.c:34 34 return 0; (gdb) x /4xb 0xbffff3c4 0xbffff3c4: 0xc8 0xf3 0xff 0xbf (gdb) x /8xb 0xbffff3c4 0xbffff3c4: 0xc8 0xf3 0xff 0xbf 0xcc 0xf3 0xff 0xbf (gdb) x /12xb 0xbffff3c4 0xbffff3c4: 0xc8 0xf3 0xff 0xbf 0xcc 0xf3 0xff 0xbf 0xbffff3cc: 0x0c 0x00 0x00 0x00
分析:
与程序2对比,可知当把b定义为指针时,b!=&b。
看懂以上两个程序,第三个也就不难理解了。 程序3:
Output: root@fc-desktop:~/Documents/sourcefile/free_test# ./free_test &strings is 0xbfee2d64 strings is 0xbfee2d64 *strings is 0x8048620
&strings[0] is 0xbfee2d64 strings[0] is 0x8048620 *strings[0] is h strings[0][0] is h
&strings[1] is 0xbfee2d68 strings[1] is 0x8048626 *strings[1] is w strings[1][0] is w |
|