3
typedef struct
{
    int idno;
    char name[max];
    float cgpa;
}student;

struct node
{
    student s;
    Link  next;
};
 typedef struct node Node;
 typedef  Node  *Link;

This doesnot work since compiler doesn't know about Link, but this works

In function 'main':| error: unknown type name 'Link'|

typedef struct {
    int idno;
    char name[max];
    float cgpa;
}student;

typedef struct node Node;
typedef  Node  *Link;
struct node
{
    student s;
    Link  next;
};

But here how does compiler knows before structure declaration and hence one could have them type defined?

Grady Player
  • 14,399
  • 2
  • 48
  • 76
bullseye
  • 81
  • 2
  • 8

2 Answers2

5
typedef struct node Node;

tells the compiler that there is a structure type with the tag node defined somewhere, and that Node is another name for that type.

typedef  Node  *Link;

tells the compiler that Link is another name for struct node *. Since all pointers to structure types are required to have the same representation and alignment requirements, that is all the compiler needs to know to use it in

struct node
{
    student s;
    Link next;
};
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • upvoting because it is correct, but I think it is much less confusing to just say `struct node{node* next;}` – Grady Player May 25 '13 at 13:50
  • Did you mean `struct node { student s; Node *next; };` or `struct node { student s; struct node *next; };`? I would prefer either to hiding the pointer in the `typedef Node *Link;` too. I understood the question as the OP wanting to have explained why it works when the typedefs are given before they are used, and thus stuck to the OP's code. – Daniel Fischer May 25 '13 at 13:58
  • good call, my forthcoming bad answer will be completely tangential! – Grady Player May 25 '13 at 14:02
0

from the C std:

A typedef declaration does not introduce a new type, only a synonym for the type so specified.

The C std doesn't seem to mention if the typedef can occur after it is used... My suspicion is that this not standard C, but a byproduct of a 2 pass compiler...

and now for the critique and ramblings section of the answer:

here are a couple of questions to ask yourself:

1) are students reusable

2) do nodes cary extra information that isn't in student...

lets assume that students are reusable, that is they can exist in more than one list... now node can not contain a whole student, because if you change one you don't change them all...

typedef struct
{
    int idno;
    char name[max];
    float cgpa;
}student;

typedef struct
{
    student * s;
    node * next;
}node;

Link is a bad name for node *, node * is a very good name that you will get used to reading, or if you must node_ptr.

If there is only one list and the nodes don't contain anything extra (not in student) then you can put the links in student:

typedef struct
{
    int idno;
    char name[max];
    float cgpa;
    student * next;
    student * prev; //if you want to be doubly linked
}student;
Grady Player
  • 14,399
  • 2
  • 48
  • 76