- 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/
我正在使用一个库 - HtmlUnit - 并且我刚刚从版本 2.4 更新到版本 2.5。当我针对 2.5 编译代码时,我收到一条我不熟悉的奇怪对象“无法取消引用”错误消息。此外,我不明白为什么当我用
我有 C 和 C++ 背景,一直认为 Java 中的解引用是通过对象的引用访问对象的过程。 例如“ref”是一个引用,当用于访问它所引用的Integer对象时,它被解引用: Integer r
这个问题已经有答案了: int cannot be dereferenced (3 个回答) 已关闭 9 年前。 我正在编写一个简单的 Java 程序来进行温度转换,并且在第 8 行(我已在下面的代码
请在说明中包含一个示例。 最佳答案 回顾基本术语 通常情况下就足够了 - 除非您正在编写程序集 - 设想一个 指针包含一个数字内存地址,其中 1 表示进程内存中的第二个字节,2 表示第三个字节,3 表
本题将从草稿N1570中获取信息,所以基本上是C11。 通俗地说,取消引用指针意味着将一元 * 运算符应用于指针。草稿文件中只有一处存在“取消引用”一词(没有“取消引用”的实例),并且位于脚注中: 1
我有这三个文件。使用结构体、函数和指针。 尝试这样做: void getName(Name *) - 接收指向名称的指针并执行操作 联系人.h struct Name { char firstNam
好的,所以我需要生成 7 个 1 到 100 之间的随机数,并将最大值打印在屏幕上。但是,当我编译此代码时: public class ArrayofTemperatures { public
我喜欢代码让我头疼的时候。除了这个。我只是使用一种常用的算法用随机 double 填充一个 vector 。问题是,在编译之后,它给了我一个“Iterator not dereferenceable”
我有 C 和 C++ 背景,一直认为 Java 中的解引用是通过对象的引用访问对象的过程。 例如“ref”是一个引用,当用于访问它所引用的Integer对象时,它被解引用: Integer r
本题将从草稿中抽取信息N1570 ,所以 C11 基本上。 通俗地说,取消引用指针意味着将一元 * 运算符应用于指针。文档草案中只有一个地方存在“取消引用”一词(没有“取消引用”的实例),并且在脚注中
我正在尝试实现一个提供 map 标记动画的库,但是当我尝试添加一些高级方法时,它在第二个“.withFillColor(Color.BLUE)”上抛出一个错误,其中显示:错误: void 无法取消引用
我收到以下代码的“int 无法取消引用”错误。任何帮助表示赞赏。谢谢: public int value() { int total = 0; **for (int i = 0; i < wallet
编译时我似乎收到“错误:int 无法取消引用”错误。我已经查找了发生这种情况的原因,尽管如此,我不确定如何解决它。 public class NetID_BaseballPitcher { S
这是我遇到问题的地方。 double num1 = Math.random(); double num2 = Math.random(); if (num1 < num2.nu
所以,我花了一些时间编写一些计算方法,以便在我正在尝试制作的角色扮演游戏的预览战斗窗口中获取一些导入数字(有人还记得《火焰纹章》吗?这是它的简化版本)。这是我尝试从这些 c 形成 jtable 的地方
我问的问题非常基础,但我试图通过在编译器中做练习来理解 C++ 中指针的行为。因此,例如,我首先声明一个 int 指针 *p 并尝试为其赋予一些值并打印它: #include using names
这里我有一些值,其中两个是整数,我不能对它们调用方法,因为它们不是引用。我该如何解决这个问题? String srcAddr, dstAddr, protocol; int srcPort, dstP
我有以下代码部分,它给出了明确的 null 取消引用。 uint64_t *var1 = NULL; char *var2 = NULL; //Alias transfer var1 = (uint6
在 C 中,是否可以为取消引用的指针定义别名/快捷方式? 即假设定义如下: void * ptr_to_my_variable = 0x2ff00000; 是否可以定义一个对应于 *ptr_to_my
假设我有这样的东西: class Collection { private: typedef std::vector >::iterator Iterator; std::vector
我是一名优秀的程序员,十分优秀!