How can I have a pointer to the next struct in the definition of this struct:
typedef struct A { int a; int b; A* next; } A;
this is how I first wrote it but it does not work.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can define the typedef and forward declare the struct first in one statement, and then define the struct in a subsequent definition.
Edit: As others have mentioned, without the forward declaration the struct name is still valid inside the struct definition (i.e. you can used
struct A), but the typedef is not available until after the typedef definition is complete (so using justAwouldn’t be valid). This may not matter too much with just one pointer member, but if you have a complex data structure with lots of self-type pointers, may be less wieldy.