我以为变量的内存地址越来越大,直到我尝试了这段代码:
#include <stdio.h>
int main()
{
int IamfirstVariable = 9;
char array1[10] = {'0','1','2','3','4','5','6','7','8','9'};
char array2[10] = {'0','1','2','3','4','5','6','7','8','9'};
char IamLastVariable = '0';
printf("variable start :%p\n",&IamfirstVariable);
printf("array1 address start :%p end : %p \n",&array1[0],&array1[9]);
printf("array2 address start :%p end : %p \n",&array2[0],&array2[9]);
printf("variable end :%p\n",&IamLastVariable);
return 0;
}
输出:
variable start :0xbfb02c3c
array1 address start :0xbfb02c32 end : 0xbfb02c3b
array2 address start :0xbfb02c28 end : 0xbfb02c31
variable end :0xbfb02c27
我被这个困住了。似乎最后声明的变量获得了最小的地址!
谁能为我解释一下?
- - - - - - - - - - - 编辑 - - - - - - - - - - - - - - -------------
我阅读了答案中的链接并得到了另一个问题:
既然堆栈向下发光,为什么数组的地址仍然向上发光?