I am not an expert C programmer and I know that including .c
source file from another is considered bad practice, but I have a situation where I think it could help maintainability.
I have a big structure with a lot of elements and I use #define
to keep the indexes.
#define TOTO_IND 0 #define TITI_IND 1 … #define TATA_IND 50 static const MyElements elems [] = { {"TOTO", 18, "French"}, {"TITI", 27, "English"}, ..., {"TATA", 45, "Spanish"} }
Since I need to access the structure from index, I need to keep the #define
and the structure declaration synchronized. That means that I must insert new elements at the right place and update the #define
accordingly.
It is error prone and I don’t really like it (but for performance consideration, I didn’t find a better solution).
Anyway, this file also contains a lot of functions to handle this structure. I also want to keep separation of code and avoid global variables.
To make things “easier”, I was thinking about moving this “error prone definition” to a single .c
source file which would only contain this structure. This file would be “the dangerous be careful file” and include it in my actual “normal functional” file.
What do you think about it? Is it a valid situation for including .c
source file? Is there another better way of handling my structure?
With implies that the person you want to provide help can decide or control on his/her own if he/she is okay with helping or not. For implies that the help this person can provide is controlled by something else and you are checking if it's okay for them to help.
Okay and OK mean the same thing. You have the answers to the questions. There's no difference between OK and okay. The older term, OK, (possibly) derived from an abbreviation for an intentional misspelling of “all correct.” The terms are both standard English.
(I'm going home.) Is that okay with you?: (I'm going home.) Do you agree? Is that a problem for you?
Okay as an adverbOkay is used as an adverb in informal speech, meaning 'all right', 'neither well nor badly': Even though I had never slept in a tent, in a sleeping bag or had any experience canoeing, I did OK.
You could use designated initializers to initialize the elements of elems[]
without having to know the explicit value of each index identifier (or macro).
const MyElements elems[] = { [TOTO_IND] = {"TOTO", 18, "French"}, [TITI_IND] = {"TITI", 27, "English"}, [TATA_IND] = {"TATA", 45, "Spanish"}, };
The array elements will be initialized the same way, even if you change the order they appear in the source code:
const MyElements elems[] = { [TITI_IND] = {"TITI", 27, "English"}, [TATA_IND] = {"TATA", 45, "Spanish"}, [TOTO_IND] = {"TOTO", 18, "French"}, };
If the array length is set automatically from the initializer as above (i.e. by using []
rather than [NUM_ELEMS]
), then the length will be the one more than the maximum element index.
This allows you to keep the index values and external declaration of the elems
array in a .h file, and define the elems
array contents in a separate .c file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With