61

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.

1
  • Note C11 §6.7.2.1 Structure and union specifiers ¶3: A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), … Commented Nov 14, 2019 at 6:34

6 Answers 6

87

You can define the typedef and forward declare the struct first in one statement, and then define the struct in a subsequent definition.

typedef struct A A;

struct A
{
    int a;
    int b;
    A* next;
};

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 just A wouldn'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.

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

7 Comments

This is the "canonical" way of doing this.
unwind: many coding style conventions for C vehemently oppose typedefing structs. I wouldn't say it's canonical at all.
If I could vote up comments, Chris Young would get a +1. There is a huge difference between native types and aggregate structures. Don't hide it behind a typedef.
@ephemient Why should I not hide it? They're just implementation details.
@turner: because one A identifies the structure type in struct A, and the second gives the type a name (alias) of A. If you had typedef int B;, then int is the type and B is the new name for the type. In the same way, with typedef struct A A;, then struct A is the type and A is the new name for the type.
|
68

In addition to the first answer, without a typedef and forward declaration, this should be fine too.

struct A 
{ 
    int a; 
    int b; 
    struct A *next; 
};

1 Comment

Plus without typedefs you actually know if you're dealing with a struct in the code
21

You are missing the struct before the A*

  typedef struct A {
    int a;
    int b;
    struct A* next;
  } A;

Comments

14

You can go without forward declaration:

struct A {
    int a;
    int b;
    struct A *next;
};

Comments

9

Please, you're in C, not C++.

If you really must typedef a struct (and most programmers that I work with would not¹), do this:

typedef struct _A {
    int a;
    int b;
    struct _A *next;
} A;

to clearly differentiate between _A (in the struct namespace) and A (in the type namespace).

¹typedef hides the size and storage of the type it points to ― the argument (and I agree) is that in a low-level language like C, trying to hide anything is harmful and counterproductive. Get used to typing struct A whenever you mean struct A.

7 Comments

How does "struct A" tell me anything about its size?
It tells you that it's an aggregate structure, and thus many things like passing it as arguments, returning it, comparisons, and a = b are inefficient or will not work.
A is a reserved name. A would be fine.
Nitpick: _A is a bad name: leading underscores belong to the compiler. edit Damn, I think I just echoed MSalters.
I have never, in all my time programming, encountered a problem from giving the struct and the typedef the same name, and then I only bother giving the struct a name in this exact case (linked lists etc). I think you are just promoting FUD. Can you give an example where it causes problems to give the same name for both? Also I have never encountered any issue by using typedef on all my structs whatsoever, so I find your argument that it is 'harmful and counterproductive' to be a bit over the top.
|
0
typedef struct {
 values
} NAME;

This is shorter way to typedef a struct i think its the easiest notation, just don't put the name infront but behind.

you can then call it like

NAME n;  

NAME *n; // if you'd like a ptr to it.

Anything wrong with this approach?

1 Comment

well as stated in the question, the need is to have a pointer INSIDE the struct (chained list) so I don't see what your answer add here.

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.