一个指向数组的第一个元素的地址? [英] Address of a pointer to first element of an array?

查看:134
本文介绍了一个指向数组的第一个元素的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对位置指向数组的开始指针的记忆混淆豆蔻。从我的理解数组和指针可以使用像*,与放大器相同的功能;和[]。所以,如果我创建一个字符数组,我的理解是:缓冲区==&放大器;缓冲区[0]

i am a litte confused by the location in memory of a pointer which points to the beginning of an array. From my understanding arrays and pointers can use the same functions like *, & and []. So if i create a char array, i understand that: buffer == &buffer[0].

因此​​,没有括号arrayName中就像它包含数组的第一个条目的地址的指针,对不对?

So the arrayname without brackets is like a pointer which contains the address of the first entry of the array, right?

但是,当我试图找出指针的地址(其中放了ADRESS;缓冲区[0]存储在),它会给我存储在指针的值相同(安培; arrayName中的[0]) 。怎么可能?如何在虚拟RAM的同一地址包含一个地址和缓冲器的值[0](这在下文等于H的code)?

But when i try to find out the address of the pointer(the adress where &buffer[0] is stored in) it will give me the same value which is stored in the pointer(&arrayname[0]). How can that be? How can the same address in virtual ram contain a address and the value of buffer[0] (which is in the code below equal to 'H')?

#include <stdio.h>
#include <windows.h>

void main() {

  char buffer[] = "Hello";

  printf("Address buffer: %d\n", &buffer);
  printf("Value buffer: %d\n", buffer);
  printf("Address buffer[0]: %d\n", &buffer[0]);
  printf("Value buffer[0]: %c\n", buffer[0]);
  printf("Address buffer[1]: %d\n", &buffer[1]);

  system("pause");
}

所以基本上我对第一个输出混淆。难道不应该从第二个被指出错误?非常感谢你对你的解释...

So basically i am confused by the first output. Shouldn't it be diffrent from the second one? Thank you very much for your explanation...

问候

推荐答案

除了当它的操作数的的sizeof 或一元&放大器; 运营商,或者正在使用字符串文字来初始化声明另一个数组,键入 T N个元素的数组的前pression 将被转换(衰变),以类型的前pression指针 T ,和前pression的价值将是该阵列的第一个元素的地址。

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

假设以下code:

char buffer[] = "Hello";
...
printf( "%s\n", buffer );

在调用的printf ,前pression 缓存有类型的第6个元素的数组字符;因为它不是的操作数的的sizeof 或一元&安培; 运营商,也不是被用来初始化另一个数组在声明中,前pression转换(衰变),以类型的前pression指针字符的char * ),和前pression的值是数组中的第一个元素的地址。

In the call to printf, the expression buffer has type "6-element array of char"; since it is not the operand of the sizeof or unary & operators, nor is it being used to initialize another array in a declaration, the expression is converted ("decays") to an expression of type "pointer to char" (char *), and the value of the expression is the address of the first element in the array.

现在,修改的printf 来电

printf( "%p", (void *) &buffer );

这一次,缓存是一元&放大器的操作; 运营商;自动转换为类型指针字符不会发生。相反,前pression类型&放大器;缓冲是指向字符的6个元素的数组,或字符(*)[6] 1 (括号问题)。

This time, buffer is the operand of the unary & operator; the automatic conversion to type "pointer to char" doesn't occur. Instead, the type of the expression &buffer is "pointer to 6-element array of char", or char (*)[6]1 (the parentheses matter).

两个前pressions产生相同的的 - 的阵列的地址是相同的数组的第一元素的地址 - 但是<青霉>类型两位前$ p $的pssions是不同的。这一点很重要; 的char * 字符(*)[6] 不能互换。

Both expressions yield the same value -- the address of the array is the same as the address of the first element of the array -- but the types of the two expressions are different. This matters; char * and char (*)[6] are not interchangeable.

那么,为什么这个时髦的转换魔法首先存在吗?

So, why does this funky conversion magic exist in the first place?

在丹尼斯里奇最初设计C,他托起设计较早的语言命名为B关于(去图)。当B中分配的数组,像这样:

When Dennis Ritchie was initially designing C, he was basing his design on an earlier language named B (go figure). When you allocated an array in B, like so:

auto arr[N];

,编译器会预留数组内容N个元素,以及与存储在阵列(基本上是一个指针值的第一个元素的偏移量额外的电池,但没有任何一种类型的语义; b是一个无类型 语言)。这种额外的电池将被绑定到变量改编,给你类似如下:

          +---+
arr:      |   | --+
          +---+   |
           ...    |
          +---+   |
arr[0]:   |   | <-+
          +---+
arr[1]:   |   |
          +---+
arr[2]:   |   |
          +---+
 ...       ...
          +---+
arr[N-1]: |   |
          +---+

里奇最初存放这些语义,但遇到了问题时,他开始把结构类型为C.他想结构类型为en code直接的字节; IOW,给定一个类型像

Ritchie initially kept these semantics, but ran into issues when he started adding struct types to C. He wanted struct types to encode their bytes directly; IOW, given a type like

struct {
  int inode;
  char name[14];
};

他想要一个2字节整数紧跟着一个14字节数组;没有一个好地方指针藏到数组的第一个元素。

he wanted a 2-byte integer immediately followed by a 14-byte array; there wasn't a good place to stash the pointer to the first element of the array.

于是他摆脱它;代替预留存储的指针数组的第一元素,他设计的语言,使得该阵列的位置将来自阵列的前pression本身来计算。因此,在这篇文章的开头的规则。

So he got rid of it; instead of setting aside storage for a pointer to the first element of the array, he designed the language so that the location of the array would be computed from the array expression itself. Hence the rule at the beginning of this post.



1。在%P 转换说明需要一个无效* 前pression作为其相应的参数,所以要转换。


1. The %p conversion specifier expects a void * expression as its corresponding argument, hence the cast.

这篇关于一个指向数组的第一个元素的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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