1

I came across typedef declaration or definition(?) while learning about linked listsin c:

typedef struct _node{
 int a;
 struct _node* next
} node;

I have no idea how it can work since the above typedef declaration's body it initiates a pointer variable with its own structure(it seems like _node is being used while defining _node). Or is there anything wrong in my understanding of what it actually does? From depiction maybe I got confused over different terms as well.

3
  • 4
    The code you presented is invalid. Could be struct _node *next;. Commented Feb 2, 2020 at 13:41
  • No doubt this is a typo. The supplied snippet does not even compile. Commented Feb 2, 2020 at 13:43
  • Yes I wrote it wrong I will edit my post thank you Commented Feb 2, 2020 at 13:58

1 Answer 1

2

Also, the type definition will only work after the struct is defined and not while defining it(C does only allow forward definitions).

This is why you cannot use node but struct node_.

In the code, you can use node because the compiler will interpret it as struct node_.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I wrote it wrong( I mean the part struct* _node = next so I edited my post. Sorry for the confusion) but u said typedef only works after struct is defined so does it mean the code above can work without predefined struct for _node??

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.