- 论坛徽章:
- 0
|
关于extern int *x与extern int y[]的区别,看了半天还是不明白,自己写程序测试:
a.c:
- #include <stdio.h>
- char arr[100]="hello world";
- extern void printadd(){
- printf("the true add:%p\n",arr);
- printf("arr[2]:%c\n",arr[2]);
- }
复制代码
- #include <stdio.h>
- extern char *arr;
- extern void printadd();
- main()
- {
- printadd();
- printf("addr:%p\n",arr);
- }
复制代码
疑问1:a.c中定义了arr[100],在b.c中extern char *arr以后两者一点关系都没有吗?程序结果:
./a
the true add:8060900
arr[2]:l
addr:6c6c6568
想知道extern char *arr到底做了些什么?
疑问2:文中一句The key point here is that the address of each symbol is known at compiletime. So if the compiler needs to do something with an address (add an offset to it, perhaps), it can do that directly and does not need to plant code to retrieve the address first. In contrast, the current value of a pointer must be retrieved at runtime before it can be dereferenced (made part of a further look-up). Diagram A shows an array reference.。对底层一些的东西还是不了解,有没有这种可能compiler分配的address被其他程序占用,这时候该程序运行会是什么样子?
[ 本帖最后由 digex 于 2006-12-30 17:32 编辑 ] |
|