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

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


当前回答

I think that the main reason that people have trouble with it is because it's generally not taught in an interesting and engaging manner. I'd like to see a lecturer get 10 volunteers from the crowd and give them a 1 meter ruler each, get them to stand around in a certain configuration and use the rulers to point at each other. Then show pointer arithmetic by moving people around (and where they point their rulers). It'd be a simple but effective (and above all memorable) way of showing the concepts without getting too bogged down in the mechanics.

一旦你学了C和c++,对某些人来说似乎就更难了。我不确定这是因为他们最终把他们没有正确掌握的理论应用到实践中,还是因为在这些语言中指针操作天生就更难。我不太记得我自己的转变,但我知道Pascal中的指针,然后转到C,完全迷失了。

其他回答

I think that the main reason that people have trouble with it is because it's generally not taught in an interesting and engaging manner. I'd like to see a lecturer get 10 volunteers from the crowd and give them a 1 meter ruler each, get them to stand around in a certain configuration and use the rulers to point at each other. Then show pointer arithmetic by moving people around (and where they point their rulers). It'd be a simple but effective (and above all memorable) way of showing the concepts without getting too bogged down in the mechanics.

一旦你学了C和c++,对某些人来说似乎就更难了。我不确定这是因为他们最终把他们没有正确掌握的理论应用到实践中,还是因为在这些语言中指针操作天生就更难。我不太记得我自己的转变,但我知道Pascal中的指针,然后转到C,完全迷失了。

在C/ c++语言中,指针为什么是许多新、甚至老大学生困惑的主要因素?

一个值的占位符的概念——变量——映射到我们在学校教的东西——代数。如果不理解内存在计算机中是如何物理布局的,就无法画出一个现有的并行图,而且没有人会考虑这种事情,直到他们处理低级别的事情——在C/ c++ /字节通信级别。

有没有什么工具或思维过程可以帮助你理解指针在变量、函数和级别之外是如何工作的?

地址框。我记得当我学习在微型计算机上编程BASIC时,有一些漂亮的书,里面有游戏,有时你必须在特定的地址中插入值。他们有一张图片,上面有一堆盒子,标有0、1、2……它解释说,只有一个小的东西(一个字节)可以装在这些盒子里,而它们有很多——一些计算机有多达65535!他们紧挨着,都有一个地址。

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

为了演习?创建一个结构体:

struct {
char a;
char b;
char c;
char d;
} mystruct;
mystruct.a = 'r';
mystruct.b = 's';
mystruct.c = 't';
mystruct.d = 'u';

char* my_pointer;
my_pointer = &mystruct.b;
cout << 'Start: my_pointer = ' << *my_pointer << endl;
my_pointer++;
cout << 'After: my_pointer = ' << *my_pointer << endl;
my_pointer = &mystruct.a;
cout << 'Then: my_pointer = ' << *my_pointer << endl;
my_pointer = my_pointer + 3;
cout << 'End: my_pointer = ' << *my_pointer << endl;

与上面的例子相同,除了在C中:

// Same example as above, except in C:
struct {
    char a;
    char b;
    char c;
    char d;
} mystruct;

mystruct.a = 'r';
mystruct.b = 's';
mystruct.c = 't';
mystruct.d = 'u';

char* my_pointer;
my_pointer = &mystruct.b;

printf("Start: my_pointer = %c\n", *my_pointer);
my_pointer++;
printf("After: my_pointer = %c\n", *my_pointer);
my_pointer = &mystruct.a;
printf("Then: my_pointer = %c\n", *my_pointer);
my_pointer = my_pointer + 3;
printf("End: my_pointer = %c\n", *my_pointer);

输出:

Start: my_pointer = s
After: my_pointer = t
Then: my_pointer = r
End: my_pointer = u

也许这通过例子解释了一些基础知识?

我认为这可能是语法问题。指针的C/ c++语法似乎不一致,而且比实际需要的更复杂。

具有讽刺意味的是,真正帮助我理解指针的是c++标准模板库中迭代器的概念。这很讽刺,因为我只能假设迭代器被认为是指针的泛化。

有时候,只有当你学会忽略树木时,你才能看到森林。

每个C/ c++初学者都有同样的问题,出现这个问题不是因为“指针很难学”,而是因为“解释指针的人以及如何解释”。有的学习者从口头或视觉上理解它,最好的解释方法是用“训练”样例(适用于口头和视觉样例)。

其中“火车头”是一个指针,它不能容纳任何东西,“马车”是“火车头”试图拉(或指向)的东西。之后,你可以对“马车”本身进行分类,它可以容纳动物、植物或人(或它们的混合)。

我喜欢用数组和下标来解释它——人们可能不熟悉指针,但他们通常都知道下标是什么。

所以我说,假设RAM是一个数组(你只有10个字节的RAM):

unsigned char RAM[10] = { 10, 14, 4, 3, 2, 1, 20, 19, 50, 9 };

然后,指向变量的指针实际上只是该变量在RAM中的第一个字节的索引。

因此,如果你有一个指针/索引unsigned char index = 2,那么这个值显然是第三个元素,或者数字4。指针指向指针的指针是指将该数字本身用作索引,如RAM[RAM[index]]。

我会在纸上的列表上画一个数组,然后用它来显示一些东西,比如指向同一个内存的许多指针、指针算术、指针到指针等等。