- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
本题将从草稿中抽取信息N1570 ,所以 C11 基本上。
通俗地说,取消引用指针意味着将一元 *
运算符应用于指针。文档草案中只有一个地方存在“取消引用”一词(没有“取消引用”的实例),并且在脚注中:
102) [...]
Among the invalid values for dereferencing a pointer by the unary
*
operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime
据我所知,一元 *
运算符实际上称为“间接运算符”,如 §6.5.3.2 所示:
6.5.3.2 Address and indirection operators
4
The unary*
operator denotes indirection. [...]
同样,它在附件§J.2中明确称为间接运算符:
— The value of an object is accessed by an array-subscript
[]
, member-access.
or−>
, address&
, or indirection*
operator or a pointer cast in creating an address constant (6.6).
那么在 C 中谈论“解引用指针”是否正确,或者这是否过于迂腐?术语从何而来? (由于 §6.5.2.1,我可以对 []
被称为“尊重”给予通行证)
最佳答案
如果看The C Programming Language,在第一版(1978 年)中,使用了术语“间接”。
例子
2.12 Precedence and Order of Evaluation
[…]
Chapter 5 discusses * (indirection) and & (address of).
,
7.2 Unary operators
[…]
The unary * operator means indirection: the expression must be a pointer, and the result is an lvalue referring to the object to which the expression points.
它也列在 INDEX 中,例如
* indirection operator 89, 187
5.1 节的较长摘录
5.1 Pointers and Addresses
Since a pointer contains the address of an object, it is possible to access the object “indirectly” through the pointer. Suppose that
x
is a variable, say anint
, and thatpx
is a pointer, created in some as yet unspecified way. The unary operator c gives the address of an object, so the statementpx = &x;
assigns the address of
x
to the variablepx; px
is now said to “point to”x
. The & operator can be applied only to variables and array elements; constructs like&(x+1 )
and&3
are illegal. It is also illegal to take the address of a register variable.The unary operator
*
treats its operand as the address off the ultimate target, and accesses that address to fetch the contents. Thus ify
is alos anint
,y = *px;
assigns to
y
the contents of whateverpx
points to. So the sequencepx = &x;
y = *px;assigns the same value to y as does
y = x;
在第二版中出现了术语解引用。
5.1 Pointers and Addresses
The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to. Suppose that x and y are integers and ip is a pointer to int. This artificial sequence shows how to declare a pointer and how to use & and *:
[…]
然而,该术语(“很多”)较旧,例如可以在
A survey of some issues concerning abstract data types , 1974. 例如 pp24/25。此处说明与 ALGOL 68、PASCAL、SIMULA 67 的连接。
The mechanism by which pointers are transformed into values by a language is known as 'dereferencing', a form of coercion (discussed later). Consider the statement
p := q;
Depending upon the types of p and q, there are several possible interpretations.
Let '@' be a dereferencing operator (i.e. if p points to j , then @p is the same as j) and '#' be a referencing operation (i.e. if p points to j , then p is the same as #j). The following table indicates the possible actions a language might take to perform the assignment:
|
| type of p
|
| t ref t ref ref t . . .
|
---------------------------------------------------------
|
t | p←q p←#q p←##q
| @p←q @p←#q
| @@p←q
type |
of |
q ref t | p←@q p←q p←#q
| @p←@q @p←q
| @@p←@q
|
|
ref ref t | p←@@q p←@q p←q
. | @p←@@q @p←@q
. | @@p←@@q
. |
|
|[…]
它的用法还有其他几个例子。虽然我无法找到它的确切位置和时间(至少现在还没有)。 (1974 年的论文至少很有趣。)
为了好玩,查看诸如 net.unix-wizards 之类的邮件列表通常也很有用。一个example from Peter Lamb at Melbourne Uni (11/28/83):
Dereferencing NULL pointers is yet another example of idiots who write 'portable' code, assuming however, that THEIR machine is the only one on which it will ever run: the same sorts of people who designed cpio with binary headers. Even on a VAX, dereferencing NULL will get you garbage: sure, *(char *)NULL and *(short *)NULL return you 0, but *(int *)NULL will give you 1024528128 !!!!.
[…]
没有提到“取消引用”,但仍然;一个有趣的读物是 Ritchie:The Development of the C Language ✝
这里也一直使用术语“间接” – 但是/和/等等。语言之间的联系有些详细。鉴于例如,该术语的使用因此很有趣。像上面提到的 1974 年的论文。
作为间接作为概念和语法的示例,例如第 12 页。
An accident of syntax contributed to the perceived complexity of the language. The indirection operator, spelled * in C, is syntactically a unary prefix operator, just as in BCPL and B. This works well in simple expressions, but in more complex cases, parentheses are required to direct the parsing.
[…]
There are two effects occurring. Most important, C has a relatively rich set of ways of describing types (compared, say, with Pascal). Declarations in languages as expressive as C– Algol 68, for example – describe objects equally hard to understand, simply because the objects themselves are complex. A second effect owes to details of the syntax. Declarations in C must be read in an ‘inside-out’ style that many find difficult to grasp [Anderson 80].
在这个结合中,可能还值得一提 ANSI C89 并提及:
3.1.2.5 Types
A pointer to void may not be dereferenced, although such a pointer may be converted to a normal pointer type which may be dereferenced.
Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, or the address of an object that has automatic storage duration when execution of the block in which the object is declared and of all enclosed blocks has terminated.
(我现在必须重新阅读其中一些文档。)
关于c - "dereferencing"这个词是从哪里来的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27445255/
我有以下案例要解决。 在短语中突出显示关键字的 Javascript 方法。 vm.highlightKeywords = (phrase, keywords) => { keywords =
我要匹配文本中的所有美元符号单词。例如,"Hello $VARONE this is $VARTWO"可以匹配$VARONE和$VARTWO。 正则表达式应该是/\$(\w+)/g,但是当我在Dart
在 redux 中,对于将状态作为参数、更改状态并返回新状态的特定操作,您会在 switch 语句中调用什么函数? function reducer(state = DEFAULT_STATE, ac
在 MySQL 5.1 中,我将一个字段命名为“Starting”。但是,每次我使用 SQL 查询时,它都会说无效的 SQL 语法。经过一些谷歌搜索,我发现 STARTING 是一个保留的 SQL 词
我必须使用函数 isIn(secretWord,lettersGuessed) 从列表中找到密码。在下面发布我的代码。 def isWordGuessed(secretWord, lettersGue
一段时间以来,我一直无法找到两个字符串中最长的常用词。首先我想到了用“isspace”函数来做这件事,但不知道如何找到一个常用词。然后我想到了“strcmp”,但到目前为止我只能比较两个字符串。我在想
我目前正在尝试制作一种“单词混合器”:对于两个给定的单词和指定的所需长度,程序应返回这两个单词的“混合”。然而,它可以是任何类型的混合:它可以是第一个单词的前半部分与第二个单词的后半部分相结合,它可以
如果 After 之后(逗号之前)没有 -ing 词,我想匹配它。所以 After 和逗号之间不应该有 -ing 词。 所需的匹配项(粗体): After sitting down, he began
我一直在试验 Stanford NLP 工具包及其词形还原功能。我很惊讶它如何使一些词词形还原。例如: depressing -> depressing depressed -> depressed
js 并尝试根据 [这里] 中的示例代码来做词云:https://github.com/jasondavies/d3-cloud .我想做的是单词的字体大小是基于数组中单词的频率。例如我有 [a,a,
我正在处理一个文本分类问题(在法语语料库上),并且正在试验不同的词嵌入。我对 ConceptNet 提供的内容非常感兴趣,所以我决定试一试。 我无法为我的特定任务找到专门的教程,所以我听取了他们的建议
当我在文本中搜索时,我输入 C-s,然后输入单词,然后一次又一次地输入 C-s,光标前进到找到的单词的下一个位置。问题是,一旦我转到下一个单词,我无法在按钮处编辑迷你缓冲区中的搜索单词,如果我按 Ba
我正在尝试按照以下结构运行这个 maven Hello Word: ├── pom.xml └── src └── Main.java 使用pom.xml设置: 4.0.0
所以,从我可以开始的.. 我正在使用 OCR。该脚本非常适合我的需要。它检测单词的准确性对我来说还可以。 这是结果:附加图像 100% 准确。 from PIL import Image import
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想要改善这个问题吗?更新问题,以便将其作为on-topi
这是细节,但我想知道为什么会这样。 示例代码: Class klasa = Enum.class; for(Type t : klasa.getGenericInterfaces()) Syst
我在用: var header = ""+ "Export HTML to Word Document with JavaScript"; var footer = ""; /
我有一个程序可以像这样将数据打印到控制台(以空格分隔): variable1 value1 variable2 value2 variable3 value3 varialbe4 value4 编辑:
我有一个程序可以像这样将数据打印到控制台(以空格分隔): variable1 value1 variable2 value2 variable3 value3 varialbe4 value4 编辑:
最近我在查看与goliath相关的一些代码时,偶然在Ruby代码中看到了这个词use。 , 中间件等。看起来它不同于include/extend, and require. 有人可以解释为什么存在这个
我是一名优秀的程序员,十分优秀!