Accessing struct from double pointer

Kevin

I'm currently trying to implement a doubly linked list in C, and am not understanding how to access the struct from a double pointer.

Here is my simple struct:

typedef struct node {
  int val;
  struct node * next;
  struct node * prev;
} node;

Here is a simple method where I try to push a value to the front of the list:

void push_front(node ** head, int newVal) 
{
  node * newNode = malloc(sizeof(node));

  newNode->val = newVal;
  newNode->next = head;

  *head->prev = newNode;

  *head = newNode;
}

However, the line *head->prev = newNode gives me an error, saying that the left of ->prev must point to a struct/union. I'm just learning C so maybe I'm overlooking something really easy, but isn't head the pointer to the pointer of my head node? And *head is the pointer to my head node. Which I would assume means *head->prev should work?

Greg Hewgill

Yes, head is a pointer to the pointer of your head node. So you can access ->prev by doing:

(*head)->prev = newNode;

Without the parentheses, the operator precedence rules of C parse your statement as

*(head->prev) = newNode;

which is not what you want.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

accessing double pointer 2d array passed by reference to a function

분류에서Dev

c++ return a pointer to a struct from a function

분류에서Dev

Accessing pointer with pointer to a literal

분류에서Dev

Accessing derived class member from base class pointer

분류에서Dev

Speed of accessing a pointer

분류에서Dev

C typedef: pointer to struct

분류에서Dev

Pointer to array of struct in function

분류에서Dev

Pointer declaration with and without struct?

분류에서Dev

Converting a double pointer to an int pointer

분류에서Dev

Segfault accessing or setting struct member

분류에서Dev

Accessing the address of an object in a pointer handle

분류에서Dev

ctypes - passing a struct with a pointer to another struct

분류에서Dev

Malloc'ing pointer-to-pointer member of struct

분류에서Dev

How to remove last node from linked list without using double pointer

분류에서Dev

Accessing struct member variables passed into function

분류에서Dev

Accessing a triple pointer inside a function with array notation

분류에서Dev

Accessing the content of a enum with an owned pointer in a for loop

분류에서Dev

accessing the member of a class of pointer array of another class

분류에서Dev

C #에서 double-C-struct-pointer를 매핑하는 방법은 무엇입니까?

분류에서Dev

C Pointer casting: single int pointer to double char pointer

분류에서Dev

Accessing winreg from jython

분류에서Dev

in C, what's the deal of casting pointer to pointer to struct ao void*

분류에서Dev

pointer / struct free ()-힙 손상

분류에서Dev

Initial typedef struct pointer (C/C++)

분류에서Dev

Returning a pointer to a struct using dynamic memory allocation

분류에서Dev

C# How to define new pointer struct?

분류에서Dev

Accessing Docker Services from Host

분류에서Dev

Accessing MacOS X clipboard from

분류에서Dev

Accessing variable from JavaScript object

Related 관련 기사

  1. 1

    accessing double pointer 2d array passed by reference to a function

  2. 2

    c++ return a pointer to a struct from a function

  3. 3

    Accessing pointer with pointer to a literal

  4. 4

    Accessing derived class member from base class pointer

  5. 5

    Speed of accessing a pointer

  6. 6

    C typedef: pointer to struct

  7. 7

    Pointer to array of struct in function

  8. 8

    Pointer declaration with and without struct?

  9. 9

    Converting a double pointer to an int pointer

  10. 10

    Segfault accessing or setting struct member

  11. 11

    Accessing the address of an object in a pointer handle

  12. 12

    ctypes - passing a struct with a pointer to another struct

  13. 13

    Malloc'ing pointer-to-pointer member of struct

  14. 14

    How to remove last node from linked list without using double pointer

  15. 15

    Accessing struct member variables passed into function

  16. 16

    Accessing a triple pointer inside a function with array notation

  17. 17

    Accessing the content of a enum with an owned pointer in a for loop

  18. 18

    accessing the member of a class of pointer array of another class

  19. 19

    C #에서 double-C-struct-pointer를 매핑하는 방법은 무엇입니까?

  20. 20

    C Pointer casting: single int pointer to double char pointer

  21. 21

    Accessing winreg from jython

  22. 22

    in C, what's the deal of casting pointer to pointer to struct ao void*

  23. 23

    pointer / struct free ()-힙 손상

  24. 24

    Initial typedef struct pointer (C/C++)

  25. 25

    Returning a pointer to a struct using dynamic memory allocation

  26. 26

    C# How to define new pointer struct?

  27. 27

    Accessing Docker Services from Host

  28. 28

    Accessing MacOS X clipboard from

  29. 29

    Accessing variable from JavaScript object

뜨겁다태그

보관