Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
To use the identifier Node without typing struct Node, you must use a typedef: typedef struct Node{ // this here is a stuct tag int data; struct Node* next; // this has to be struct Node...
Answer
#3: Post edited
- To use the identifier `Node` without typing `struct Node`, you must use a `typedef`:
- typedef struct Node{ // this here is a stuct tag
- int data;
- struct Node* next; // this has to be struct Node, refers to the above tag
- }Node; // but from this line and onward we can use Node
- `typedef` and struct tags reside in different namespaces, why we can use the same identifier `Node` twice for seemingly unrelated purposes. Since a linked list node needs to be self-referring, we have to use the struct tag so the compiler knows what to do until the struct type definition is completed at the final `;`
- Another equivalent alternative would be a forward declaration:
- typedef struct Node Node; // forward declaration inside a typedef
struct Node{ // struct tag- int data;
- Node* next; // now the compiler knows what Node is before the struct is complete
- };
- Note that C++ is different here.
- To use the identifier `Node` without typing `struct Node`, you must use a `typedef`:
- typedef struct Node{ // this here is a stuct tag
- int data;
- struct Node* next; // this has to be struct Node, refers to the above tag
- }Node; // but from this line and onward we can use Node
- `typedef` and struct tags reside in different namespaces, why we can use the same identifier `Node` twice for seemingly unrelated purposes. Since a linked list node needs to be self-referring, we have to use the struct tag so the compiler knows what to do until the struct type definition is completed at the final `;`
- Another equivalent alternative would be a forward declaration:
- typedef struct Node Node; // forward declaration inside a typedef
- struct Node{ // struct tag to associate this with the above typedef
- int data;
- Node* next; // now the compiler knows what Node is before the struct is complete
- };
- Note that C++ is different here.
#2: Post edited
- To use the identifier `Node` without typing `struct Node`, you must use a `typedef`:
- typedef struct Node{ // this here is a stuct tag
- int data;
- struct Node* next; // this has to be struct Node, refers to the above tag
- }Node; // but from this line and onward we can use Node
- Note that C++ is different here.
- To use the identifier `Node` without typing `struct Node`, you must use a `typedef`:
- typedef struct Node{ // this here is a stuct tag
- int data;
- struct Node* next; // this has to be struct Node, refers to the above tag
- }Node; // but from this line and onward we can use Node
- `typedef` and struct tags reside in different namespaces, why we can use the same identifier `Node` twice for seemingly unrelated purposes. Since a linked list node needs to be self-referring, we have to use the struct tag so the compiler knows what to do until the struct type definition is completed at the final `;`
- Another equivalent alternative would be a forward declaration:
- typedef struct Node Node; // forward declaration inside a typedef
- struct Node{ // struct tag
- int data;
- Node* next; // now the compiler knows what Node is before the struct is complete
- };
- Note that C++ is different here.
#1: Initial revision
To use the identifier `Node` without typing `struct Node`, you must use a `typedef`: typedef struct Node{ // this here is a stuct tag int data; struct Node* next; // this has to be struct Node, refers to the above tag }Node; // but from this line and onward we can use Node Note that C++ is different here.