为什么指针是许多刚开始学习C或c++,甚至上了大学的学生困惑的主要因素?有没有什么工具或思维过程可以帮助你理解指针在变量、函数和级别之外是如何工作的?
有什么好的实践可以让人达到“啊哈,我懂了”的水平,而不会让他们陷入整体概念中?基本上,就是模拟场景。
为什么指针是许多刚开始学习C或c++,甚至上了大学的学生困惑的主要因素?有没有什么工具或思维过程可以帮助你理解指针在变量、函数和级别之外是如何工作的?
有什么好的实践可以让人达到“啊哈,我懂了”的水平,而不会让他们陷入整体概念中?基本上,就是模拟场景。
当前回答
当我只懂c++的时候,我可以使用指针。从试错中,我知道在某些情况下应该做什么,不应该做什么。但是让我完全理解的是汇编语言。如果您对自己编写的汇编语言程序进行了一些严肃的指令级调试,那么您应该能够理解很多东西。
其他回答
上面的一些回答断言“指针并不难”,但并没有直接解决“指针难”的来源。几年前,我辅导一年级的CS学生(只教了一年,因为我显然学得很差),我很清楚指针的概念并不难。难的是理解为什么以及什么时候需要指针。
我不认为您可以将这个问题(为什么以及何时使用指针)与解释更广泛的软件工程问题分开。为什么每个变量都不应该是全局变量,以及为什么应该把类似的代码分解成函数(也就是使用指针将它们的行为专门化到它们的调用位置)。
通过迭代器来掌握它是个不错的方法。但继续看,你会发现亚历山大开始抱怨他们。
许多前c++开发人员(在抛弃语言之前从未理解迭代器是一个现代指针)跳转到c#,仍然相信他们有不错的迭代器。
嗯,问题是所有迭代器都与运行时平台(Java/CLR)试图实现的目标完全不一致:新的、简单的、人人都是dev的用法。这可能是好事,但他们在紫书里说过一次,甚至在C之前说过
间接。
这是一个非常强大的概念,但如果你一直这样做,就不会如此了。迭代器很有用,因为它们有助于算法的抽象,这是另一个例子。编译时是算法的地方,非常简单。你知道代码+数据,或者用其他语言c#:
IEnumerable + LINQ + Massive Framework = 300MB运行时惩罚间接的糟糕,拖动应用程序通过引用类型的实例堆..
“Le Pointer很便宜。”
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.
指针重载了。指针是指向值的地址吗?或者它是一个将地址保存到值的变量。当一个函数需要一个指针时,它是想要指针变量保存的地址,还是指针变量的地址? 我困惑。
这种混淆来自于在“指针”概念中混合在一起的多个抽象层。程序员不会对Java/Python中的普通引用感到困惑,但指针的不同之处在于它们暴露了底层内存架构的特征。
清晰地分离抽象层是一个很好的原则,而指针做不到这一点。
我不明白指针有什么好困惑的。它们指向内存中的一个位置,也就是存储内存地址的位置。在C/ c++中,你可以指定指针所指向的类型。例如:
int* my_int_pointer;
表示my_int_pointer包含指向包含int类型的位置的地址。
指针的问题在于它们指向内存中的某个位置,因此很容易回溯到不应该在的某个位置。作为证明,看看C/ c++应用程序中大量的安全漏洞,这些漏洞来自缓冲区溢出(指针的增量超过分配的边界)。