5

Code without typedef (and it works):

struct Node {
    int data;
    struct Node *next;
    struct Node *prev;
};

I'm trying to make a code using typedef for the structure "Node" in Doubly Linked List, but this does not work:

typedef struct {
    int data;
    Node *next;
    Node *prev;
} Node;

Is there a way around this using typedef?

3 Answers 3

8

You can use a forward declaration of the struct

typedef struct sNode Node; // this is a typedef and a declaration of the struct
struct sNode{
    int data;
    Node *next;
    Node *prev;
};

This way Node is known (but not defined), in the definition of your struct.

This can be compressed as it is done by Yunnosch. But then you need to use the struct Name notation inside your declaration.

This way it is possible to already use the typedefed name also the forward declaration is necessary if you have some circular dependencies in your structs.

It is also possible to use the struct name as the typedef:

typedef struct Node Node; 
struct Node{
    int data;
    Node *next;
    Node *prev;
};

I personally prefer the first style, it seems "clearer" to me, but there is nothing wrong with the second example, as long as the compiler is not from the pre-standard era (before 1989).

As Jens Gustedt pointed out the first style might be incompatible if this is included in C++.

So maybe I should change my preference to the first.

9
  • 1
  • @JHBonarius Nice link, I added it to my SO link-library.
    – Yunnosch
    Jun 15, 2018 at 6:46
  • There is not much reason to use struct sNode. struct Node, that is the same identifier for the tag as for the typedef is sufficient. Jun 15, 2018 at 6:47
  • 1
    @JensGustedt As it seems there might even be some problems with this practice. Thanks to JHBonarius
    – Kami Kaze
    Jun 15, 2018 at 7:08
  • 1
    Just to put that into context, pre-standard refers to compilers that have not got around to become standard conforming for about 30 years. And no, this is not dirty, this is just the way it should be. Having different names for the same type is highly confusing, and typedeffing to the same identifier is the thing to do if you want to be header compatible with C++. Jun 15, 2018 at 8:14
6

Inside the typedef, the to-be-defined type is not yet known, so you need to introduce and use a struct tag:

typedef struct Node_tag {
    int data;
    struct Node_tag *next;
    struct Node_tag *prev;
} Node;

The Node_tag is the struct tag, because of where it is introduced (and not because of the name part "_tag"). It is not a type in itself, only the combination as struct Node_tag is a type which can be used for the struct members.
Afterwards, when the typedef is done, the type Node has been defined.
To clarify, a second typedef would be possible as typedef struct Node_tag NodeToo;. This demonstrates that the type struct Node_tag is also useable. That is why I prefer to use the "_tag" name fragment, to allow to be clear of what is used.

0
1

Inside the structure, you have indeed to use types that are already known by the compiler. A way to be compliant with this rule in your case is to use the struct Node * type after having declared typedef struct Node. In this case, even if the type struct Node is incompletely defined when the compiler encounters it inside the structure, the name is known and the compiler knows the size of a pointer (to reserve in memory), so it has no reason to complain !
Morevover it is also accepted to use the same name for the type as after struct: you can use Node for both.
Here is my suggested solution:

typedef struct Node {
    int data;
    struct Node *next;
    struct Node *prev;
} Node;
5
  • Although supported, I don't consider it good practice to give the struct the same name as the type. It's discouraged for compatibility (with ancient compilers
    – JHBonarius
    Jun 15, 2018 at 7:04
  • @Laurent Okay, thank you! Unfortunately, I've chosen to pick another answer. Nevertheless, your explanation helps a lot and I've upvoted your answer :-)
    – Richard
    Jun 15, 2018 at 7:26
  • @WealthyPlayer You're welcome, thanks for your upvote. My answer was indeed a bit late, but I tried it to be complete. Glad to see that it was helpful !
    – Laurent H.
    Jun 15, 2018 at 7:44
  • @WealthyPlayer Generally you are not bound to your first decision of the selected answer. You should pick what you think is appropiate.
    – Kami Kaze
    Jun 15, 2018 at 8:37
  • @Kami Noted :-D Sorry for the very late comment response.
    – Richard
    Jun 24, 2018 at 6:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.