我在c中有这段代码:
int q = 10;
int s = 5;
int a[3];
printf("Address of a: %d\n", (int)a);
printf("Address of a[1]: %d\n", (int)&a[1]);
printf("Address of a[2]: %d\n", (int)&a[2]);
printf("Address of q: %d\n", (int)&q);
printf("Address of s: %d\n", (int)&s);
Run Code Online (Sandbox Code Playgroud)
输出是:
Address of a: 2293584
Address of a[1]: 2293588
Address of a[2]: 2293592
Address of q: 2293612
Address of s: 2293608
Run Code Online (Sandbox Code Playgroud)
所以,我看到,从那里a
开始a[2]
,内存地址每个增加4个字节.但是,从q
到s
,内存地址减少了4个字节.
我想知道两件事:
a[2]
和q
内存地址之间发生了什么?为什么那里存在很大的记忆差异?(20个字节).注意:这不是作业问题.我很好奇堆栈是如何工作的.谢谢你的帮助.