Basically the compiler complains of unknown type name 'Song_t' since the datatype definition happens after it is first referenced.
struct Cell { Song_t song;
struct Cell *pnext;
};
typedef struct Song Song_t;
If I place the typedef before, it works. Is it therefore generally advised to place typedefs at the very beginning of each file?
Is it therefore generally advised to place typedefs at the very beginning of each file?
Not necessarily, and you could avoid typedef
-s by coding
struct Cell {
struct Song song;
struct Cell *pnext;
};
Of course, struct Song
should be defined "before" (take into account the C preprocessor).
You could also code
typedef struct Song Song_t;
typedef struct Cell Cell_t;
and use later only Song_t
etc. You do have to provide (later) a definition of struct Song
(not just a forward declaration).
For details, read Modern C, see this C reference website, and the C11 standard n1570.
Look for inspiration into the source code of the Linux kernel or of a simple C compiler, such as nwcc, or of the GTK toolkit. All these are coded (mostly) in C. Look also for examples on github. Study also the source code of GCC. It is a popular C compiler (and old versions of it -e.g. GCC 4.4- have been coded in C mostly).