使用指向指针的指针访问结构成员
我有一个类似于下面的程序,当我尝试使用指针指针访问结构成员时,它说该表达式必须具有指向类型的指针。请告诉我如何使用指针指针访问结构对象的数据元素
#include "stdafx.h"
#include<iostream>
struct Node{
int data;
Node* next;
};
void addnode(Node** node, int val)
{
if (node == NULL){
*node = new Node();
*node->data = val;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Node* node;
addnode(&node, 10);
return 0;
}
I have a program like below, when I try to access a struct member using pointer to pointer it says the expression must have a pointer to class type. Please tell me how can I access data element of the struct object using a pointer to pointer
#include "stdafx.h"
#include<iostream>
struct Node{
int data;
Node* next;
};
void addnode(Node** node, int val)
{
if (node == NULL){
*node = new Node();
*node->data = val;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Node* node;
addnode(&node, 10);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在
**node-&gt; data = val;
中涉及3个操作员。操作员*
derference操作员 - &gt;
访问成员operator =
进行分配,这些顺序会发生哪种顺序?根据操作员优先级 precedce
code> pertor> pertor> pertor> pertors-&gt-&gt&gt&gt ;
运算符*
操作员=
node-&gt; data
将首先发生,然后将结果取消结果 - 因此,您在左侧有*(node-&gt; data)
。您需要先解释
节点
首先可以使用(
...)
覆盖优先级规则:另请注意您的原始
节点*
是非初始化的,并且读取它(如果(node == null)在中所做的就像您在
中所做的那样)会导致程序具有不确定的行为。将其初始化为
nullptr
:In
*node->data = val;
there are 3 operators involved.operator*
to dereferenceoperator->
to access a memberoperator=
to do the assignmentIn which order will these happen? According to operator precedence which states:
operator->
operator*
operator=
node->data
will happen first and then the result will be dereferenced - so you have*(node->data)
on the left hand side.You need to dereference
node
first and can use(
...)
to override the precedence rules:Also note that your original
Node*
is uninitialized and reading it (like you do inif(node == NULL)
) will cause the program to have undefined behavior. Initialize it tonullptr
:*node-&gt; data = val;
等于*(node-&gt; data)= val;
,但是您想要的是(*node) - &gt; data = val;
(因此您需要添加parens)。*node->data = val;
is equivalent to*(node->data) = val;
, but what you want is(*node)->data = val;
(so you need to add the parens).这是操作的顺序。它需要为
(*node) - &gt; data = val
。另请注意,如果
语句正在做与您想要的相反:*node
不要成为错误,您需要测试node
is 不是 null并不是因为它为null。It's an order of operations thing. It needs to be
(*node)->data = val
. Also note that theif
statement is doing the opposite of what you want:*node
to not be an error you need to test thatnode
is not null not that it is null.对于初学者,您需要在主要中初始化指针节点
,其次,由于操作员的优先级,因此该表达式
等于
,因为后缀运算符(包括运算符
- &gt;
)的优先级比单位运算符(如Dereference Operator)更高*
。至少您需要写作
,但无论如何,该功能没有意义。
如果您需要在单链接列表的开头添加新节点,则该函数将以下方式看起来以下方式
或不使用双重指针,例如
在这种情况下,函数会像
您想将新节点附加到末尾的函数 一样。然后在这种情况下,单一连接的列表看起来像
For starters you need to initialize the pointer node in main
Secondly this expression
is equivalent to
due to the precedence of operators because postfix operators including the operator
->
have a higher precedence than unary operators like the dereference operator*
.At least you need to write
But in any case the function does not make a sense.
If you need to add a new node to the beginning of the singly linked list then the function will look the following way
Or without using double pointers like
In this case the function is called like
If you want to append a new node to the end of the singly-linked list then in this case the function can look like
问题是由于操作员优先> 语句
*node-&gt; data = val;
等效于:上述语句未工作是因为DERERECTING
节点
将为我们提供Node*
,它没有任何名为data
的成员,因为它是非类型类型。到求解您需要通过在
*Node
添加括号来覆盖此行为,如下所示:此外,变量
node
Insidemain main
是非初始化的。您应该将其初始化,如下所示:The problem is that due to operator precedence the statement
*node->data = val;
is equivalent to:The above statement doesn't work because dereferencing
node
will give usNode*
which does not have any member calleddata
as it is a non-class type.To solve this you need to override this behavior by adding parenthesis around
*node
as shown below:Additionally, the variable
node
insidemain
is uninitialized. You should initialize it as shown below: