Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static const structs in C

We are starting a rather big project that involves parts in different languages. We decided to have a certain part translated at compile-time from a JSON to the necessary C structs (and other structures for the rest of the system parts).

The struct is defined in a .h, and the declaration will be like const static struct MCSCommandOptionsMessage mcs_command_message_list[] = and then list of parameters in the format required.

The question is: should the auto generated file with the struct declaration be a .h, or is it better to have a .c just for this struct? Why?

like image 377
markmb Avatar asked Mar 03 '26 21:03

markmb


1 Answers

Both would be possible, declaration in .h or in the only one .c file the has access to the struct. If the struct defines something like an Interface and/or you generate several structs if would be good to have them in a .h file. It would define a contract. If several instances of the struct exist you have to put the struct definition in a .h file.

If you would to express Isolation and emphasize that the struct is private matter you can define it in the .c file.

like image 81
harper Avatar answered Mar 05 '26 11:03

harper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!