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

查看:93
本文介绍了为什么会告诉我数组是指针? 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#for 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 ++是根据抽象内存模型设计的,但是在大多数情况下,这直接转换为系统操作系统甚至更低层提供的内存模型
  • 内存分为称为 bytes (通常为8位)的基本单位
  • 可以将内存分配为对象的存储;例如当您写入int x;时,决定将相邻字节的特定块放在一边以存储整数值. object 是已分配存储空间的任何区域. (是的,这是一个稍微循环的定义!)
  • 分配的存储的每个字节都有一个地址,该地址是一个令牌(通常表示为简单数字),可用于在内存中查找该字节.对象内任何字节的地址必须是连续的.
  • 名称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 ++的类型系统使用强类型,这意味着每个对象都有一个 type .类型系统扩展到指针.在几乎所有情况下,用于存储指针的存储区仅保存所指向对象的第一个字节的地址.在编译时使用类型系统来跟踪所指向的内容.这就是为什么我们有不同类型的指针(例如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天全站免登陆