Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';t指针指向指针,无需强制转换即可访问结构成员? 在C++中,我试图通过双指针头文件>代码>访问结构成员,如下面的 struct Node{ int data; struct Node* next; }; struct Node* head = new Node; head->data = 10; head->next = NULL; struct Node** headref = &head;_C++_Pointers_Operator Precedence_Unary Operator - Fatal编程技术网

为什么可以';t指针指向指针,无需强制转换即可访问结构成员? 在C++中,我试图通过双指针头文件>代码>访问结构成员,如下面的 struct Node{ int data; struct Node* next; }; struct Node* head = new Node; head->data = 10; head->next = NULL; struct Node** headref = &head;

为什么可以';t指针指向指针,无需强制转换即可访问结构成员? 在C++中,我试图通过双指针头文件>代码>访问结构成员,如下面的 struct Node{ int data; struct Node* next; }; struct Node* head = new Node; head->data = 10; head->next = NULL; struct Node** headref = &head;,c++,pointers,operator-precedence,unary-operator,C++,Pointers,Operator Precedence,Unary Operator,但是,访问as*headref->data会在强制转换数据时产生错误。为什么?这个表达式 *headref->data ( *headref )->data ((Node*)*headref)->data 相当于 *( headref->data ) 它与有效表达式不同 *headref->data ( *headref )->data ((Node*)*headref)->data 因为数据成员data没有指针类型,您不能对其应用一元运算

但是,访问as
*headref->data
会在强制转换数据时产生错误。为什么?

这个表达式

*headref->data
( *headref )->data
((Node*)*headref)->data
相当于

*( headref->data )
它与有效表达式不同

*headref->data
( *headref )->data
((Node*)*headref)->data
因为数据成员
data
没有指针类型,您不能对其应用一元运算符*

这句话

*headref->data
( *headref )->data
((Node*)*headref)->data
由于铸造原因无效。它是有效的,因为如果要删除多余的强制转换,您将获得有效的edxpression

( /*(Node*)*/ *headref)->data

如上所示。

如果将运算符链接在一起,最好确保正确记住它们的优先级。如上所列,
运算符->
的优先级高于间接
*
。因此

*headref->data
被解释为

*(headref->data)
这是行不通的。相反,使用

(*headref)->data

这相当于
((Node*)headref)->数据

你能在你的问题中包含错误吗?你面临什么样的错误?。请注意,
->
的优先级高于
*
这不是强制转换。如果您认为是演员阵容,请尝试
((Node*)headref)->数据
,而不是
((Node*)headref)->数据
。它应该失败得惊人。正如你所写的,演员阵容是不允许的。@WhozCraig,谢谢。优先权是个问题。啊,这个问题解决了。谢谢我将检查运算符优先级。