使用指向指针的指针访问结构成员

发布于 2025-01-18 01:11:18 字数 425 浏览 4 评论 0原文

我有一个类似于下面的程序,当我尝试使用指针指针访问结构成员时,它说该表达式必须具有指向类型的指针。请告诉我如何使用指针指针访问结构对象的数据元素

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

献世佛 2025-01-25 01:11:18

**node-&gt; data = val;中涉及3个操作员。

  • 操作员* derference
  • 操作员 - &gt;访问成员
  • operator =进行分配,

这些顺序会发生哪种顺序?根据操作员优先级 precedce

precepedence操作员
2code> pertor> pertor> pertor> pertors-&gt-&gt&gt&gt ;
3运算符*
16操作员=

node-&gt; data将首先发生,然后将结果取消结果 - 因此,您在左侧有*(node-&gt; data)

您需要先解释节点首先可以使用 ... 覆盖优先级规则:

(*node)->data = val;

另请注意您的原始节点*是非初始化的,并且读取它(如果(node == null)在中所做的就像您在中所做的那样)会导致程序具有不确定的行为。将其初始化为nullptr

Node* node = nullptr; // here
addnode(&node, 10);

In *node->data = val; there are 3 operators involved.

  • operator* to dereference
  • operator-> to access a member
  • operator= to do the assignment

In which order will these happen? According to operator precedence which states:

PrecedenceOperator
2operator->
3operator*
16operator=

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:

(*node)->data = val;

Also note that your original Node* is uninitialized and reading it (like you do in if(node == NULL) ) will cause the program to have undefined behavior. Initialize it to nullptr:

Node* node = nullptr; // here
addnode(&node, 10);
北方。的韩爷 2025-01-25 01:11:18

*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).

缘字诀 2025-01-25 01:11:18

这是操作的顺序。它需要为(*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 the if statement is doing the opposite of what you want: *node to not be an error you need to test that node is not null not that it is null.

百变从容 2025-01-25 01:11:18

对于初学者,您需要在主要中初始化指针节点

Node* node = nullptr;

,其次,由于操作员的优先级,因此该表达式

*node->data = val;

等于

* ( node->data ) = val;

,因为后缀运算符(包括运算符- &gt;)的优先级比单位运算符(如Dereference Operator)更高*

至少您需要写作

( *node )->data = val;

,但无论如何,该功能没有意义。

如果您需要在单链接列表的开头添加新节点,则该函数将以下方式看起来以下方式

void addnode(Node** node, int val)
{
    *node = new Node { val, *node };
}

或不使用双重指针,例如

void addnode( Node* &node, int val)
{
    node = new Node { val, node };
}

在这种情况下,函数会像

addnode(node, 10);

您想将新节点附加到末尾的函数 一样。然后在这种情况下,单一连接的列表看起来像

void addnode(Node** node, int val)
{
    while ( *node ) node = &( *node )->next;
    *node = new Node { val, *node };
}

For starters you need to initialize the pointer node in main

Node* node = nullptr;

Secondly this expression

*node->data = val;

is equivalent to

* ( node->data ) = val;

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

( *node )->data = val;

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

void addnode(Node** node, int val)
{
    *node = new Node { val, *node };
}

Or without using double pointers like

void addnode( Node* &node, int val)
{
    node = new Node { val, node };
}

In this case the function is called like

addnode(node, 10);

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

void addnode(Node** node, int val)
{
    while ( *node ) node = &( *node )->next;
    *node = new Node { val, *node };
}
夏末染殇 2025-01-25 01:11:18

问题是由于操作员优先> 语句*node-&gt; data = val;等效于:

*(node->data) = val;

上述语句未工作是因为DERERECTING 节点将为我们提供Node*,它没有任何名为data的成员,因为它是非类型类型。
求解您需要通过在*Node添加括号来覆盖此行为,如下所示:

(*node)->data = val;

此外,变量node Inside main main是非初始化的。您应该将其初始化,如下所示:

 Node* node = nullptr;

The problem is that due to operator precedence the statement *node->data = val; is equivalent to:

*(node->data) = val;

The above statement doesn't work because dereferencing node will give us Node* which does not have any member called data as it is a non-class type.
To solve this you need to override this behavior by adding parenthesis around *node as shown below:

(*node)->data = val;

Additionally, the variable node inside main is uninitialized. You should initialize it as shown below:

 Node* node = nullptr;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文