为什么指针不在堆栈上?

use*_*466 3 c pointers

void main(){
    int i,k;
    char* p;
    int j;
    printf("address of i is %d \naddress of k is %d \naddress of p is %p\naddress of j is %d", &i,&k,&p,&j);

}
Run Code Online (Sandbox Code Playgroud)

当我尝试上面的代码时,j的地址比k低4个单位.但是p的地址不在附近.由于指针是一个可以存储4个字节数据的整数变量,为什么不像其他三个变量一样在堆栈上分配?

Mic*_*kis 13

使用%p打印所有地址.

扰流板:它在栈上.


Mic*_*urr 11

你应该发布你得到的输出.我怀疑你有点困惑,因为你正在打印的大多数地址都以十进制显示(使用%d),而p以十六进制显示(使用%p).


Pup*_*ppe 6

我刚刚在我的计算机上运行了你的代码(运行Ubuntu 9.04)并获得了以下内容:

address of i is 0xbf96fe30
address of k is 0xbf96fe2c
address of p is 0xbf96fe28
address of j is 0xbf96fe24
Run Code Online (Sandbox Code Playgroud)

稍微更改代码后:

void main(){
    int i,k;
    char* p;
    int j;
    printf("address of i is %p \naddress of k is %p \naddress of p is %p\naddress of j is %p\n", &i,&k,&p,&j);

}
Run Code Online (Sandbox Code Playgroud)

由于你所有的printf()都是地址,你应该使用%p而不是%d.也许你误解了你的结果?