为什么指针是许多刚开始学习C或c++,甚至上了大学的学生困惑的主要因素?有没有什么工具或思维过程可以帮助你理解指针在变量、函数和级别之外是如何工作的?

有什么好的实践可以让人达到“啊哈,我懂了”的水平,而不会让他们陷入整体概念中?基本上,就是模拟场景。


当前回答

在我的第一节compp Sci课上,我们做了以下练习。当然,这是一个大约有200名学生的演讲厅……

教授在黑板上写道:int john;

约翰站起来

教授写道:int *sally = &john;

莎莉站起来,指着约翰

int *bill = sally;

比尔站起来,指着约翰

教授:int sam;

山姆站起来

教授:bill = &sam;

比尔现在指向山姆。

我想你已经明白了。我想我们花了一个小时来做这个,直到我们复习了指针赋值的基础知识。

其他回答

指针让很多人感到困惑的原因是它们大多没有计算机架构背景。由于许多人似乎不知道计算机(机器)是如何实际实现的——在C/ c++中工作似乎很陌生。

一个练习是要求他们实现一个简单的基于字节码的虚拟机(在任何他们选择的语言中,python都很适合这个),其中的指令集集中于指针操作(加载、存储、直接/间接寻址)。然后要求他们为该指令集编写简单的程序。

任何需要比简单加法稍微多一点的东西都会涉及到指针,它们肯定会得到指针。

一个带有一组很好的图表的教程示例极大地帮助理解指针。

Joel Spolsky在他的《面试指南》文章中提出了一些关于理解指针的好观点:

出于某种原因,大多数人似乎生来就没有大脑中理解指针的那部分。这是一个天赋问题,而不是技能问题——它需要一种复杂的双重间接思维形式,而有些人就是做不到。

The problem with pointers is not the concept. It's the execution and language involved. Additional confusion results when teachers assume that it's the CONCEPT of pointers that's difficult, and not the jargon, or the convoluted mess C and C++ makes of the concept. So vast amounts of effort are poored into explaining the concept (like in the accepted answer for this question) and it's pretty much just wasted on someone like me, because I already understand all of that. It's just explaining the wrong part of the problem.

为了让你知道我是怎么来的,我是一个非常理解指针的人,我可以在汇编语言中熟练地使用它们。因为在汇编语言中,它们不被称为指针。它们被称为地址。当我在C语言中编程和使用指针时,我犯了很多错误,并感到非常困惑。我还没弄清楚。让我给你们举个例子。

当一个api说:

int doIt(char *buffer )
//*buffer is a pointer to the buffer

它想要什么?

它可能想要:

表示缓冲区地址的数字

(给它,我说doIt(mybuffer),还是doIt(* mybuffer) ?)

表示缓冲区地址的一种数字

(doIt(&mybuffer) or doIt(mybuffer) or doIt(*mybuffer)?)

表示缓冲区地址的地址的数字

(可能是doIt(&mybuffer)。还是doIt(&&mybuffer) ?甚至doIt(&&&mybuffer))

and so on, and the language involved doesn't make it as clear because it involves the words "pointer" and "reference" that don't hold as much meaning and clarity to me as "x holds the address to y" and "this function requires an address to y". The answer additionally depends on just what the heck "mybuffer" is to begin with, and what doIt intends to do with it. The language doesn't support the levels of nesting that are encountered in practice. Like when I have to hand a "pointer" in to a function that creates a new buffer, and it modifies the pointer to point at the new location of the buffer. Does it really want the pointer, or a pointer to the pointer, so it knows where to go to modify the contents of the pointer. Most of the time I just have to guess what is meant by "pointer" and most of the time I'm wrong, regardless of how much experience I get at guessing.

指针重载了。指针是指向值的地址吗?或者它是一个将地址保存到值的变量。当一个函数需要一个指针时,它是想要指针变量保存的地址,还是指针变量的地址? 我困惑。

我喜欢家庭地址的比喻,但我一直认为地址是邮箱本身。通过这种方式,您可以可视化解除指针引用(打开邮箱)的概念。

例如在一个链表下面: 1)论文开头写上地址 2)去纸上的地址 3)打开邮箱,找到一张新纸,上面写着下一个地址

在线性链表中,最后一个邮箱中没有任何内容(列表的末尾)。在循环链表中,最后一个邮箱具有其中第一个邮箱的地址。

请注意,第3步是发生解引用的地方,当地址无效时,您将崩溃或出错。假设你可以走到一个无效地址的邮箱前,想象那里有一个黑洞或什么东西,把世界翻个底朝天:)

我不明白指针有什么好困惑的。它们指向内存中的一个位置,也就是存储内存地址的位置。在C/ c++中,你可以指定指针所指向的类型。例如:

int* my_int_pointer;

表示my_int_pointer包含指向包含int类型的位置的地址。

指针的问题在于它们指向内存中的某个位置,因此很容易回溯到不应该在的某个位置。作为证明,看看C/ c++应用程序中大量的安全漏洞,这些漏洞来自缓冲区溢出(指针的增量超过分配的边界)。