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

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


当前回答

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.

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

其他回答

我发现Ted Jensen的“C中的指针和数组教程”是学习指针的极好的资源。它分为10节课,从解释指针是什么(以及它们是用来干什么的)开始,到以函数指针结束。http://web.archive.org/web/20181011221220/http://home.netcom.com:80/~tjensen/ptr/cpoint.htm

接着,Beej的《网络编程指南》教授了Unix套接字API,从中你可以开始做一些真正有趣的事情。http://beej.us/guide/bgnet/

起初,我很难理解指针的原因是,许多解释都包含了很多关于引用传递的废话。所有这些都混淆了问题。当你使用指针形参时,你仍然是通过值传递;但是这个值恰好是一个地址,而不是int型。

其他人已经链接到本教程,但我可以强调我开始理解指针的时刻:

C语言指针和数组教程:第3章-指针和字符串

int puts(const char *s);

目前,忽略const。传递给puts()的形参是一个指针,即指针的值(因为C中的所有形参都是按值传递的),指针的值是它所指向的地址,或者简单地说,是一个地址。因此,当我们写put (strA);正如我们所看到的,我们正在传递strA[0]的地址。

当我读到这些文字的那一刻,乌云散开,一束阳光带着指教般的理解将我笼罩。

即使你是一个VB . net或c#开发人员(就像我一样)并且从未使用过不安全的代码,了解指针的工作原理仍然是值得的,否则你将无法理解对象引用的工作原理。然后您就会有一个常见但错误的概念,即将对象引用传递给方法会复制对象。

它之所以如此难以理解,并不是因为它是一个复杂的概念,而是因为语法不一致。

int *mypointer;

您首先了解到变量创建的最左边部分定义了变量的类型。在C和c++中,指针声明不是这样工作的。相反,他们说变量指向左边的类型。在这种情况下:*mypointer指向int类型。

我没有完全掌握指针,直到我尝试在c#中使用它们(不安全),它们以完全相同的方式工作,但具有逻辑和一致的语法。指针本身就是一个类型。这里mypointer是一个指向int型的指针。

int* mypointer;

甚至不要让我开始函数指针…

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

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

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

邮政信箱号码。

它是一条信息,允许你访问其他东西。

(如果你计算邮政信箱号码,你可能会有问题,因为信进了错误的信箱。如果有人搬到另一个州——没有转发地址——你就有一个悬浮指针。另一方面,如果邮局转发邮件,那么你就有了指向指针的指针。)