取数组地址时不同的指针算术结果 [英] Different Pointer Arithmetic Results when Taking Address of Array

查看:78
本文介绍了取数组地址时不同的指针算术结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序:

#include<stdio.h>

int main(void) {
    int x[4];
    printf("%p\n", x);
    printf("%p\n", x + 1);
    printf("%p\n", &x);
    printf("%p\n", &x + 1);
}

输出:

$ ./a.out
0xbff93510
0xbff93514
0xbff93510
0xbff93520
$

我希望以下是上述程序的输出.例如:

I expect that the following is the output of the above program. For example:

x        // 0x100
x+1      // 0x104  Because x is an integer array
&x       // 0x100  Address of array
&x+1     // 0x104

但是最后一条语句的输出与我期望的不同. &x也是数组的地址.所以在这个上增加1 将打印以4递增的地址.但是&x+1给出以10递增的地址.为什么?

But the output of the last statement is different from whast I expected. &x is also the address of the array. So incrementing 1 on this will print the address incremented by 4. But &x+1 gives the address incremented by 10. Why?

推荐答案

x -> Points to the first element of the array.
&x ->Points to the entire array.

偶然发现了以下描述性解释:

Stumbled upon a descriptive explanation here: http://arjunsreedharan.org/post/69303442896/the-difference-between-arr-and-arr-how-to-find

SO链接:为什么arr和& arr相同?

这篇关于取数组地址时不同的指针算术结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆