0

Can someone explain this piece of code for me?

typedef char AirportCode[4];

typedef struct NodeTag{
  AirportCode Airport;
  struct NodeTag *link;
}Node;

Node *L;

This is a snippet of code from my data structures class. This piece of code was implemented to hold data in a linkedlist node. What I'm trying to understand is what the NodeTag, in general, is used for? Is it when you want the structure to hold another iteration, for the lack of a better word, of the struct inside itself? What is the difference between the NodeTag and what you put after the curly brace, in this case "Node"?

Thomas Baruchel
  • 7,236
  • 2
  • 27
  • 46
  • After defining NodeTag like that instead of using "struct NodeTag varName" to create a variable we can just use "Node varName". So it's just trying to avoid writing "struct" keyword. – 11thdimension Nov 28 '15 at 05:52
  • 1
    @Martin I don't think that's really a duplicate -- the OP here is asking about `NodeTag` vs. `Node`, whereas the example in your proposed dupe use unnamed structs. If anything, the proposed dupe of that question ([Difference between 'struct' and 'typedef struct' in C++?](http://stackoverflow.com/q/612328/643383)) is a better choice except that it covers C++ instead of C. – Caleb Nov 28 '15 at 06:31

1 Answers1

5

You're combining two things into one here:

(1) The struct declaration:

struct NodeTag{
  AirportCode Airport;
  struct NodeTag *link;
};

which would require you to declare your Nodes like this:

struct NodeTag *nt; 

Anything after the closing curly brace of the struct definition would declare a variable of that type.

struct foo {int n;} a_global_variable;

With (2) a typedef:

typedef ... Node;

which allows you to simply say:

Node *n; 

However, inside the struct declaration, since the typedef does not yet exist, you must still say struct NodeTag *nt rather than Node *n

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36
  • 4
    Note that **some** structure tag (here, `NodeTag`) is required in this case, because the structure definition is recursive. However, it is legal and very common to use the same identifier for both the structure tag and the type alias, e.g. `typedef struct Node { ... } Node;`. – rob mayoff Nov 28 '15 at 05:54
  • Late in the code it goes... Node *L, *temp; L = (Node*)malloc(sizeof(Node)); L->Link = NULL; temp = (Node*)malloc(sizeof(Node)); L->Link = temp; How is this possible if L->link is of type NodeTag and temp is of Type Node. I'm trying to implement something similar to another program i'm working on and im using this code as something as a guide and I keep getting a compatibility error. – Andy Steven Llactahuamani Nov 28 '15 at 07:02
  • @AndyStevenLlactahuamani because they're the same type, technically :) the typedef is just an aliasing mechanism. Although it makes sense you'll get a warning, you may have to do a cast - since you know as a programmer that they are identical, it's not the best practice, but you can cast accordingly. – Ricky Mutschlechner Nov 28 '15 at 08:29