C Preprocessor - Add Elements to Struct at Compile Time

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


C Preprocessor - Add Elements to Struct at Compile Time



I am trying to think of a way to add elements to a struct at compile time, but by defining this in another file. For example:



defA.h:


typedef struct A {
int element1;
int element2;
} A;



otherfile.c:


#include "defA.h"

typedef struct B {
int element1;
} B;

ADD_ELEMENT_TO(A, B, element3)



Would result in:


struct A {
int element1;
int element2;
B element3;
};



Can anyone think of a way to achieve this or something similar? I want to be able to control this by choosing to compile otherfile.c or not with the rest of the build.


otherfile.c





What problem are you trying to solve?
– OldProgrammer
3 mins ago





It is not unusual to want the contents of some structures to depend on conditional compilation, but most often it is simply done with #if statements that simply include the desired members or not. Or one might define a macro that conditional expands to declarations of the desired members or to an empty string and then use that macro in the structure declaration. Are things like that sufficient for you? If not, why do you want to try this more complicated method; what goal would it accomplish for you?
– Eric Postpischil
2 mins ago


#if









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results