Home > OS >  accessing struct members using pointer to pointer
accessing struct members using pointer to pointer

Time:03-31

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;
}

CodePudding user response:

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:

Precedence Operator
2 operator->
3 operator*
16 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:

(*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);

CodePudding user response:

*node->data = val; is equivalent to *(node->data) = val;, but what you want is (*node)->data = val; (so you need to add the parens).

CodePudding user response:

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;

CodePudding user response:

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.

CodePudding user response:

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 };
}
  • Related