cstructdeclarationtypedefincomplete-type

Are forward declarations needed when the typedef declaration is done?


#ifndef HANDLE_H
#define HANDLE_H

struct List;
typedef struct List* HANDLE;

struct Person;
typedef struct Person Person_t;

HANDLE list_create(void);
void list_destroy(HANDLE);
void list_push_front(HANDLE, const Person_t*);
void list_pop_front(HANDLE);

#endif  // HANDLE_H

Question: Are the forward declarations (List and Person) needed, and is it a good practice to forward declare them?


Solution

  • Forward declarations are only needed when the struct definition isn't already visible earlier inside the same translation unit or otherwise you wouldn't be able to use the struct. Forward declarations can be done with or without typedef. Meaning that the struct Person; etc parts are redundant here.

    Regarding best practices: