为什么我被告知数组是指针?C++中数组和指针的关系是什么? [英] Why am I being told that an array is a pointer? What is the relationship between arrays and pointers in C++?

查看:25
本文介绍了为什么我被告知数组是指针?C++中数组和指针的关系是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景是 C++,我目前正准备开始用 C# 进行开发,所以我正在做一些研究.然而,在这个过程中,我遇到了一些对 C++ 提出问题的问题.

My background is C++ and I'm currently about to start developing in C# so am doing some research. However, in the process I came across something that raised a question about C++.

这个C# 开发人员指南 表示

在 C++ 中,数组只是一个指针.

In C++ an array is merely a pointer.

但是这个 StackOverflow 问题 有一条高票评论说

But this StackOverflow question has a highly-upvoted comment that says

数组不是指针.不要再告诉别人了.

Arrays are not pointers. Stop telling people that.

cplusplus.com 上的指针页面 说数组和指针是相关的(并提到了隐式转换,所以它们显然相同).

The cplusplus.com page on pointers says that arrays and pointers are related (and mentions implicit conversion, so they're obviously not the same).

数组的概念与指针的概念有关.事实上,数组的工作方式非常类似于指向它们的第一个元素的指针,实际上,数组总是可以隐式转换为正确类型的指针.

The concept of arrays is related to that of pointers. In fact, arrays work very much like pointers to their first elements, and, actually, an array can always be implicitly converted to the pointer of the proper type.

我的印象是,Microsoft 页面想要简化事情以总结 C++ 和 C# 之间的差异,并在此过程中编写了一些更简单但并非 100% 准确的内容.

I'm getting the impression that the Microsoft page wanted to simplify things in order to summarise the differences between C++ and C#, and in the process wrote something that was simpler but not 100% accurate.

但是数组首先与指针有什么关系?为什么关系密切到可以概括为相同",即使他们不是?

But what have arrays got to do with pointers in the first place? Why is the relationship close enough for them to be summarised as the "same" even if they're not?

cplusplus.com 页面说数组像"指向它们的第一个元素的指针一样工作.如果它们实际上不是指向其第一个元素的指针,那意味着什么?

The cplusplus.com page says that arrays "work like" pointers to their first element. What does that mean, if they're not actually pointers to their first element?

推荐答案

有很多不好的写作.例如声明:

There is a lot of bad writing out there. For example the statement:

在 C++ 中,数组只是一个指针.

In C++ an array is merely a pointer.

简直是假的.怎么会写出这么烂的文章?我们只能推测,但一种可能的理论是,作者使用编译器通过反复试验来学习 C++,并根据他的实验结果形成了错误的 C++ 心智模型.这可能是因为 C++ 用于数组的语法非常规.

is simply false. How can such bad writing come about? We can only speculate, but one possible theory is that the author learned C++ by trial and error using a compiler, and formed a faulty mental model of C++ based on the results of his experiments. This is possibly because the syntax used by C++ for arrays is unconventional.

下一个问题是,学习者如何知道他/她阅读的是好材料还是坏材料?当然,除了阅读我的帖子 ;-) 之外,参与像 Stack Overflow 这样的社区有助于让你接触到许多不同的演示和描述,然后一段时间后你就有足够的信息和经验来做出自己的决定哪个写得好,哪个写得不好.

The next question is, how can a learner know if he/she is reading good material or bad material? Other than by reading my posts of course ;-) , participating in communities like Stack Overflow helps to bring you into contact with a lot of different presentations and descriptions, and then after a while you have enough information and experience to make your own decisions about which writing is good and which is bad.

回到数组/指针主题:我的建议是,当我们在 C++ 中工作时,首先建立一个关于对象存储如何工作的正确思维模型.仅仅为这篇文章写可能太多了,但我将如何从头开始构建它:

Moving back to the array/pointer topic: my advice would be to first build up a correct mental model of how object storage works when we are working in C++. It's probably too much to write about just for this post, but here is how I would build up to it from scratch:

  • C 和 C++ 是根据抽象内存模型设计的,但在大多数情况下,这会直接转换为系统操作系统或更低层提供的内存模型
  • 内存被划分为称为字节(通常为 8 位)的基本单位
  • 内存可以分配为对象的存储空间;例如当您编写 int x; 时,决定留出一个特定的相邻字节块来存储整数值.对象 是已分配存储的任何区域.(是的,这是一个稍微循环的定义!)
  • 分配存储的每个字节都有一个地址,它是一个标记(通常表示为一个简单的数字),可用于在内存中查找该字节.对象内任何字节的地址必须是连续的.
  • 名称x 只存在于程序的编译阶段.在运行时,可以分配 int 对象,但它们从来没有名字;并且在编译期间可以有其他具有一个或多个名称的 int 对象.
  • 所有这些都适用于任何其他类型的对象,而不仅仅是 int
  • 一个数组是一个由多个相邻的相同类型的子对象组成的对象
  • 指针是一个对象,用作标识可以在何处找到另一个对象的标记.
  • C and C++ are designed in terms of an abstract memory model, however in most cases this translates directly to the memory model provided by your system's OS or an even lower layer
  • The memory is divided up into basic units called bytes (usually 8 bits)
  • Memory can be allocated as storage for an object; e.g. when you write int x; it is decided that a particular block of adjacent bytes is set aside to store an integer value. An object is any region of allocated storage. (Yes this is a slightly circular definition!)
  • Each byte of allocated storage has an address which is a token (usually representible as a simple number) that can be used to find that byte in memory. The addresses of any bytes within an object must be sequential.
  • The name x only exists during the compilation stage of a program. At runtime there can be int objects allocated that never had a name; and there can be other int objects with one or more names during compilation.
  • All of this applies to objects of any other type, not just int
  • An array is an object which consists of many adjacent sub-objects of the same type
  • A pointer is an object which serves as a token identifying where another object can be found.

从这里开始,C++ 语法就出现了.C++ 的类型系统使用强类型,这意味着每个对象都有一个类型.类型系统扩展到指针.几乎在所有情况下,用于存储指针的存储都只保存所指向对象的第一个字节的地址;并且在编译时使用类型系统来跟踪所指向的内容.这就是为什么我们有不同类型的指针(例如 int *float *),尽管在这两种情况下存储可能包含相同类型的地址.

From hereon in, C++ syntax comes into it. C++'s type system uses strong typing which means that each object has a type. The type system extends to pointers. In almost all situations, the storage used to store a pointer only saves the address of the first byte of the object being pointed to; and the type system is used at compilation time to keep track of what is being pointed to. This is why we have different types of pointer (e.g. int *, float *) despite the fact that the storage may consist of the same sort of address in both cases.

最后:所谓的数组指针等价"不是存储的等价,如果您理解我的最后两个要点.它是查找数组成员的等效语法.

Finally: the so-called "array-pointer equivalence" is not an equivalence of storage, if you understood my last two bullet points. It's an equivalence of syntax for looking up members of an array.

既然我们知道一个指针可以用来寻找另一个对象;数组是一系列相邻的对象;然后我们可以通过使用指向该数组第一个元素的指针来处理该数组.等效的是,相同的处理可用于以下两种情况:

Since we know that a pointer can be used to find another object; and an array is a series of many adjacent objects; then we can work with the array by working with a pointer to that array's first element. The equivalence is that the same processing can be used for both of the following:

  • 查找数组的第 N 个元素
  • 在我们查看的对象之后在内存中查找第 N 个对象

此外,这些概念可以使用相同的语法表达.

and furthermore, those concepts can be both expressed using the same syntax.

这篇关于为什么我被告知数组是指针?C++中数组和指针的关系是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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