Base Operand of ‘->’ Has Non-Pointer Type

First, the code:

// ...

struct node_list {
    node_list *prev;
    node *target;     // node is defined elsewhere in the application
    node_list *next;
    };

node_list nl_head;

int main() {
    nl_head->prev = &nl_head;
    // ...
    return 0;
    }

I get an error:

make (in directory: #####)
g++ -Wall -std=c++11 -o main main.cc
main.cc: In function ‘int main(int, char**)’:
main.cc:38:9: error: base operand of ‘->’ has non-pointer type ‘node_list’
  nl_head->prev = &nl_head;
         ^
Makefile:8: recipe for target 'main' failed
make: *** [main] Error 1
Compilation failed.

As far as I can tell my syntax is correct. Can anyone point out the error?

Before anyone flags it as a duplicate, I am aware it is similar to a couple other questions but none of their solutions seem to work for me. Unless I'm doing it wrong, which I'll admit is possible, but that's why I'm here.

3 Answers

nl_head is not a pointer. try nl_head.prev

1

As suggested by the error message and your question title. nl_head is not a pointer so you cannot use the -­> operator.

Make it a pointer. You will also need to allocate memory before you can use it.

Alternatively, you can not make it a pointer but instead use the dot operator to access its member.

nl_head is an object of the node_list structure, it is not a pointer, so use the dot operator and assign the loa:

nl_head.prev = &nl_head;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest