Search code examples
c++cstructstandardsstandards-compliance

C struct definition standard


I'm programming in C in the last few years and I developed an habit to put the typedef of the struct in the header file, and to leave its definition in the source file, so others can use my files without letting them mess things up.

// header file
typedef struct s_example EXAMPLE

// source file
struct s_example {
       // something
};

is there something wrong it doing this, should i let others know what i do with my structs?

im just asking this because i see a lot of people showing everything in the header file.


Solution

  • The downside of only having the declaration in the header file is that all other source files that do not include the definition of the struct cannot instantiate an instance of that struct, but only a pointer to it. This is because, to those other source files, your struct is an incomplete type. If that is fine for your application, then there is nothing wrong with just a declaration in the header.