c - 取数组地址时指针运算结果不同

标签 c pointers language-lawyer

程序:

#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
$

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

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 的地址。为什么?

最佳答案

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

在这里偶然发现了一个描述性的解释:http://arjunsreedharan.org/post/69303442896/the-difference-between-arr-and-arr-how-to-find

SO 链接:Why is arr and &arr the same?

关于c - 取数组地址时指针运算结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775701/

相关文章:

c - "cast specifies a conversion"是什么意思?

c++ - 我相信短语 "not previously declared to have external linkage"在 N4140 的§3.5/3 项目符号点 (3.2) 中是多余的

c++ - 在 Cygwin 上构建 Nodejs 时出现构建错误

C - 如果结构在文件中如何保留数组

c - 指针到指针算术未按预期运行

c - 通过使用一系列指针从结构访问字符串

c - 修复不兼容的指针类型警告

c - OpenCL 原子不执行任何操作

c - 在C中的字符串中为每个单词的结尾添加空格

c++ - 为什么在 C++ 中允许多个预增量但在 C 中不允许?