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

查看:21
本文介绍了如何向初学者解释 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
", (void *)&foo);
printf("%i
", *bar);

对于绝对的初学者来说,输出可能会令人惊讶.在第 2 行中,他/她刚刚将 *bar 声明为 &foo,但在第 4 行中,结果证明 *bar 实际上是 foo 而不是 &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 旨在作为助记符;它说表达式 *ip 是一个整数.变量声明的语法模仿可能出现该变量的表达式的语法.

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 will return an int"?但是为什么声明之后的赋值不遵循这种模式呢?如果初学者想初始化变量怎么办?int *ip = 1 (阅读:*ip 将返回一个 intint1) 不会按预期工作.概念模型似乎并不连贯.我在这里遗漏了什么吗?

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?

它试图在这里总结答案.

推荐答案

为了让您的学生理解 * 符号在不同上下文中的含义,他们必须首先了解上下文确实是不同的.一旦他们了解上下文不同(即作业的左侧和一般表达式之间的差异),理解这些差异是什么并不是太多的认知飞跃.

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.

首先解释变量的声明不能包含运算符(通过展示在变量声明中放置-+ 符号只会导致错误来证明这一点).然后继续证明表达式(即在赋值的右侧)可以包含运算符.确保学生理解表达式和变量声明是两个完全不同的上下文.

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.

当他们理解上下文不同时,你可以继续解释当*符号在变量标识符前面的变量声明中时,它的意思是'声明这个变量为指针'.然后你可以解释一下,当在表达式中使用时(作为一元运算符),* 符号是解引用运算符",它的意思是地址处的值"而不是它之前的含义.

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天全站免登陆