0

Why list node is pointer type what's difference will be without pointer?

typedef struct node {
    char data;
    struct node *nextptr;
} node_t;
    
typedef node_t *listnode;
Dai
  • 141,631
  • 28
  • 261
  • 374
rosterrz
  • 35
  • 8
  • Is this C or C++? – Dai Mar 12 '22 at 11:03
  • this code is in c language – rosterrz Mar 12 '22 at 11:24
  • 1
    Does this answer your question? [Is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Dai Mar 12 '22 at 11:26
  • actually its not (typedef node_t *listnode;) this is for linked list why listnode is pointer type thats my question? – rosterrz Mar 12 '22 at 11:31
  • 1
    "why listnode is pointer type" because `typedef node_t* listnode` means "define `listnode` as a type-alias for `node_t*` (the `*` is part of the left-hand-side _source type_, not the right-hand-side type-alias name) (which is why I personally always keep the `*` on the left, not the right). [Also read this](https://stackoverflow.com/questions/180401/placement-of-the-asterisk-in-pointer-declarations). – Dai Mar 12 '22 at 11:33

2 Answers2

0

it is pointer type because you have n elements, and for every element you now it data, and pointer for next element. n you may not know, so you use dynamic array of your structures. you can declare you your array using *. and after that you can give him memory. if you know n, it may be typedef node_t listnode[n];

Alexander
  • 31
  • 6
0

In dynamic allocation of memory, you need a pointer to store the base address of memory space allotted. Thus declaring a pointer to the structure of type node_t helps you to go with dynamic memory allocation.

mintu
  • 1
  • 1
    so int is pointer type only because we can dynamically able to allocate space. And for every node memory of structure will be continuous. – rosterrz Mar 12 '22 at 12:30