什么是数组变量的地址获取意思? [英] What does getting address of array variable mean?

查看:159
本文介绍了什么是数组变量的地址获取意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天看了一个C段这实际上混淆了我:

 的#include<&stdio.h中GT;INT
主要(无效)
{
    诠释一个[] = {0,1,2,3};    的printf(%d个\\ N,*(*(&安培A + 1) - 1));
    返回0;
}

在我看来,&安培; A + 1 是没有意义的,但它运行没有错误。

可能有人请解释一下这是什么意思,谢谢。
而且确实K&放大器; R C圣经覆盖了本

UPDATE0:
看完答案后,我意识到,这两个前pressions主要是混淆了我:


  1. &安培A + 1 ,一直在问SO:的有关前pression&放大器; anArray在C


  2. *(安培A + 1)-1 ,这是腐烂的相关阵列



解决方案

让我们来仔细分析它。

A 的类型 INT [4] (4整型数组)。它的尺寸为 4 * sizeof的(INT)

&放大器;一个已键入 INT(*)[4] (指向4整型数组)。

(安培A + 1)也有键入 INT(*)[4] 。它指向4一个int数组启动 1 *的sizeof(A)字节(或 4 * sizeof的(INT)开始 A 后字节)。

*(安培A + 1)的类型为 INT [4] (一个4整型数组)。这是存储启动 1 *的sizeof(A)字节(或 4 * sizeof的(INT)开始后的字节 A

*(安培A + 1) - 1 的类型为为int * (指针为int),因为数组 *(安培A + 1)衰变到一个指向它的第一个元素在这个前pression。这将指向启动 1 *的sizeof(INT)字节 *开始前一个int(安培A + 1)。这是相同的指针值&放大器;一个[3]

*(*(&安培A + 1) - 1)的类型为 INT 。因为 *(安培A + 1) - 1 是相同的指针值&放大器;一个[3] *(*(&安培A + 1) - 1)等同于 A [3] ,已初始化为 3 ,所以这是由打印数的printf

Today I read a C snippet which really confuses me:

#include <stdio.h>

int
main(void)
{
    int a[] = {0, 1, 2, 3};

    printf("%d\n", *(*(&a + 1) - 1));
    return 0;
}

In my opinion, &a + 1 makes no sense, but it runs without error.

Could someone please explain what it means, thanks. And does K&R C Bible covers this?

UPDATE0: After reading the answers, I realize that these two expressions mainly confuses me:

  1. &a + 1, which has been asked in SO: about the expression "&anArray" in c

  2. *(&a + 1) -1, which is related to array decaying.

解决方案

Let's dissect it.

a has type int [4] (array of 4 int). It's size is 4 * sizeof(int).

&a has type int (*)[4] (pointer to array of 4 int).

(&a + 1) also has type int (*)[4]. It points to an array of 4 int that starts 1 * sizeof(a) bytes (or 4 * sizeof(int) bytes) after the start of a.

*(&a + 1) is of type int [4] (an array of 4 int). It's storage starts 1 * sizeof(a) bytes (or 4 * sizeof(int) bytes after the start of a.

*(&a + 1) - 1 is of type int * (pointer to int) because the array *(&a + 1) decays to a pointer to its first element in this expression. It will point to an int that starts 1 * sizeof(int) bytes before the start of *(&a + 1). This is the same pointer value as &a[3].

*(*(&a + 1) - 1) is of type int. Because *(&a + 1) - 1 is the same pointer value as &a[3], *(*(&a + 1) - 1) is equivalent to a[3], which has been initialized to 3, so that is the number printed by the printf.

这篇关于什么是数组变量的地址获取意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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