Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to #include .c source file for maintainability of embedded C code?

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?

like image 505
ncenerar Avatar asked Oct 11 '18 10:10

ncenerar


People also ask

Is that OK for you or with you?

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.

Is it okay or it is okay?

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.

Is it okay with you meaning?

(I'm going home.) Is that okay with you?: (I'm going home.) Do you agree? Is that a problem for you?

Is Okay informal?

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.


1 Answers

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.

like image 140
Ian Abbott Avatar answered Sep 22 '22 23:09

Ian Abbott