如何解释C指针(声明与一元运算符),以一个初学者? [英] How to explain C pointers (declaration vs. unary operators) to a beginner?

查看:89
本文介绍了如何解释C指针(声明与一元运算符),以一个初学者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有快感最近解释指向一个C编程初学者,偶然发现了以下困难。这似乎是一个问题可能不是所有的,如果你已经知道如何使用指针,但尝试看看下面的例子有一个清醒的头脑:

I have had the recent pleasure to explain pointers to a C programming beginner and stumbled upon the following difficulty. It might not seem like an issue at all if you already know how to use pointers, but try to look at the following example with a clear mind:

int foo = 1;
int *bar = &foo;
printf("%p\n", (void *)&foo);
printf("%i\n", *bar);

要绝对初学者的输出可能会令人惊讶。在第2行,他/她刚刚宣布*酒吧是与放大器;富,但在第4行原来* bar实际上foo的,而不是和放大器;!富

To the absolute beginner the output might be surprising. In line 2 he/she had just declared *bar to be &foo, but in line 4 it turns out *bar is actually foo instead of &foo!

混乱,你可能会说,从*符号的模糊性源于:在2号线它是用来声明指针。在第4行它用作一元运算符而在取的值的指针指向。两个不同的东西,对吧?

The confusion, you might say, stems from the ambiguity of the * symbol: In line 2 it is used to declare a pointer. In line 4 it is used as an unary operator which fetches the value the pointer points at. Two different things, right?

不过,这个解释并不能帮助初学者在所有。它介绍了通过指出一个微妙的差异的新概念。这不可能是教的正确方法。

However, this "explanation" doesn't help a beginner at all. It introduces a new concept by pointing out a subtle discrepancy. This can't be the right way to teach it.

所以,没Kernighan和Ritchie如何解释呢?

So, how did Kernighan and Ritchie explain it?

单目运算符*是间接或对其操作;当应用于一个指针,它访问该对象的指针指向。 [...]

The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. […]

指针IP的声明,为int * IP 的目的是作为一个记忆;它说,前pression * IP 是一个int。 的声明中变模仿的语法前pressions的语法变量在其中可能会出现

The declaration of the pointer ip, int *ip is intended as a mnemonic; it says that the expression *ip is an int. The syntax of the declaration for a variable mimics the syntax of expressions in which the variable might appear.

为int * IP 应该读像 * IP 将返回 INT ?但是,为什么那么不宣布后的分配遵循这一模式?如果有什么初学者要初始化变量? 为int * IP = 1 (阅读: * IP 将返回 INT INT 1 )将无法正常工作。概念模型只是看起来不连贯。我失去了一些东西在这里?

int *ip should be read like "*ip will return an int"? But why then doesn't the assignment after the declaration follow that pattern? What if a beginner wants to initialize the variable? int *ip = 1 (read: *ip will return an int and the int is 1) won't work as expected. The conceptual model just doesn't seem coherent. Am I missing something here?

编辑:它试图这里总结了答案

It tried to summarize the answers here.

推荐答案

有关你的学生理解的含义 * 在不同环境下的符号,他们必须首先明白,上下文确实是不同的。一旦他们明白,背景是不同的(即赋值的左侧和一般前pression之间的差)是没有太多认知飞跃了解这些差异是什么。

For your student to understand the meaning of the * symbol in different contexts, they must first understand that the contexts are indeed different. Once they understand that the contexts are different (i.e. the difference between the left hand side of an assignment and a general expression) it isn't too much of a cognitive leap to understand what the differences are.

首先解释变量的声明不能包含运营商(通过表明把一个证明这一点 - + 在一个变量声明符号只是导致错误)。然后去证明一个前pression(即在赋值的右边)可以包含运营商。确保学生明白,一个前pression和变量的声明是两个完全不同的上下文。

Firstly explain that the declaration of a variable cannot contain operators (demonstrate this by showing that putting a - or + symbol in a variable declaration simply causes an error). Then go on to show that an expression (i.e. on the right hand side of an assignment) can contain operators. Make sure the student understands that an expression and a variable declaration are two completely different contexts.

当他们明白,上下文是不同的,你可以去解释,当 * 符号是在变量标识符前面的变量声明,这意味着声明此变量的指针。然后,你可以解释一下,在一个前pression使用时,(作为一元运算符)的 * 符号是'反引用运算符',它的意思是在价值的地址,而不是其先前的意义。

When they understand that the contexts are different, you can go on to explain that when the * symbol is in a variable declaration in front of the variable identifier, it means 'declare this variable as a pointer'. Then you can explain that when used in an expression (as a unary operator) the * symbol is the 'dereference operator' and it means 'the value at the address of' rather than its earlier meaning.

要真正说服你的学生,解释C的创造者也可以使用任何符号来表示的引用操作(即他们可以使用 @ 代替),但不管是什么他们之所以做出设计决定使用 *

To truly convince your student, explain that the creators of C could have used any symbol to mean the dereference operator (i.e. they could have used @ instead) but for whatever reason they made the design decision to use *.

所有的一切,还有周围解释说,环境是不同的没办法。如果学生不理解语境是不同的,他们无法理解为什么 * 符号可能意味着不同的事情。

All in all, there's no way around explaining that the contexts are different. If the student doesn't understand the contexts are different, they can't understand why the * symbol can mean different things.

这篇关于如何解释C指针(声明与一元运算符),以一个初学者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆