- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近有幸向 C 编程初学者解释指针,并偶然发现了以下困难。如果您已经知道如何使用指针,这可能看起来根本不是问题,但请尝试以清醒的头脑看下面的示例:
int foo = 1;
int *bar = &foo;
printf("%p\n", (void *)&foo);
printf("%i\n", *bar);
对于绝对初学者来说,输出可能会令人惊讶。在第 2 行,他/她刚刚声明 *bar 为 &foo,但在第 4 行,事实证明 *bar 实际上是 foo 而不是 &foo!
您可能会说,混淆源于 * 符号的歧义:在第 2 行中,它用于声明指针。在第 4 行中,它用作一元运算符,用于获取指针指向的值。两个不同的东西,对吧?
但是,这种“解释”对初学者一点帮助都没有。它通过指出细微的差异引入了一个新概念。这不是正确的教学方法。
那么,Kernighan 和 Ritchie 是如何解释的?
The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. […]
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
) 将无法按预期工作。概念模型似乎并不连贯。我在这里遗漏了什么吗?
编辑:它试图summarize the answers here .
最佳答案
简写的原因:
int *bar = &foo;
在您的示例中可能会造成混淆,因为很容易将其误读为等同于:
int *bar;
*bar = &foo; // error: use of uninitialized pointer bar!
当它实际上意味着:
int *bar;
bar = &foo;
这样写,变量声明和赋值分开,就没有这种混淆的可能性,并且在您的 K&R 引用中描述的 use ↔ 声明并行性非常有效:
第一行声明了一个变量bar
,这样*bar
就是一个int
。
第二行将foo
的地址赋值给bar
,使得*bar
(一个int
) foo
的别名(也是 int
)。
在向初学者介绍 C 指针语法时,最初坚持这种将指针声明与赋值分开的风格可能会有所帮助,并且只有在基本概念C 中的指针使用已充分内化。
关于c - 如何向初学者解释 C 指针(声明与一元运算符)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27484168/
我不明白为什么我会从 GHCi 得到以下回复。不是Maybe构造函数? Prelude> :t Maybe :1:1: Not in scope: data constructor `Maybe' P
场景是我在此网站上有不同的访问级别,我有一个适用于所有有效用户的简单登录流程,但是我现在尝试分隔不同的用户以实现对页面的不同访问。 这是我页面开头的代码: // CHECKS IF THE USER
我的任务是:写下数字1-100。如果该数字可以被 3 整除,则将其写入数字“它可以被 3 整除”旁边的控制台。如果数字是 5,也将其写入数字旁边的控制台“它可以被 5 整除”,如果它不能被 3 整除,
我有一堆实现协议(protocol) P 的记录 (A B C)。 我想编写一个方法,该方法将选择一种记录类型,构造它,然后调用它的方法。 例如,如果我有一个记录列表(def types '(A B
我的任务是编写一个程序,根据以下三个因素来预测您的年度燃料使用量汽车加油。我必须使用两个单独的类。这是我的第一个类,名为 AnnualFuelUse。 public class AnnualFuelU
我是 JavaScript 新手。我已经通过 Learning JavaScript (o'reilly) 完成了我的工作,但我只是想制作我的第一个 JavaScript。 我认为最好从事我感兴趣的事
我真的刚刚开始学习如何用 Python 编写代码。我有兴趣 如何重现 u[x,t] 矩阵。我尝试了 return u,它抛出了一个错误。 如果此代码中 for 循环的位置正确并正常运行。 最重要的是,
我不明白 view("") 在作为 Model 对象一部分的以下 javascript 方法中的作用: addView: function(view) { this.views
所以我正在尝试将 AppKit 导入到我的 python 项目中。我正在使用 pyCharm,但每次我尝试导入时,都会收到以下错误消息: You are using pip version 6.0.8
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 2 年前。 Improve
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
好吧,不知道在哪里问这个,但我是一个初学者程序员,使用 Perl。我需要创建一个数组数组,但我不确定使用数组/哈希引用、哈希数组或数组哈希等是否更好。 我需要一组匹配项:@totalmatches 每
我最近开始学习 PHP。我做了一个基本的网站,基本上想给它密码。如果有人能告诉我为什么这不起作用,我将不胜感激。我知道它不起作用,因为我已经尝试过了;我只是不明白为什么。 ... REST OF W
我试图理解 C 中的整个指针和取消引用。我几乎明白了,但遇到了非常简单的代码,结果我不明白: char *ptr = "Characters"; char val = *ptr; char *chrp
首先,我有这个列表(在练习中建议): Members = [('Tessa','G1'),('Evan','G2'),('Tom','G3'), ('Mia','G3'),('Claire','G3'
我有以下列表: listofanimals = ['Dog', 'Cat', 'Frog', 'Tiger', 'Sheep', 'Lion'] 我想根据字典对这个列表进行排序: score_card
1 userID = floatval($userID); 13 } else { 14 $this->userID = floatval(
我是 R 新手,遇到了一段我不理解的代码。更具体地说,我想知道 .Internal做。这是我尝试转换为 Matlab 的示例: dunif 我想知道.Internal和 做。 非常感谢您, 西蒙 最
我是一名优秀的程序员,十分优秀!