I have this code defined in the 3rd party library I'm using:
typedef enum {
STATE_INITIAL = 0,
STATE_LOAD = 1,
STATE_READ = 2,
STATE_FINISH = 3
} state_t;
I would like to re-define the value of STATE_FINISH. However, I don't want to mess with the library, and at the same time I can't do it this way in my code:
#undef STATE_FINISH
#define STATE_FINISH 2
Is there another way?
First of all, the #undef will not have an effect. That only works on names that have been #defined. So you should leave that out.
Second, the #define STATE_FINISH 2 would likely have the effect that you seem to want. You must however be sure that it is always used in all cases where the header is #included. Otherwise, the value that is used in your program for STATE_FINISH will be inconsistent. Also you must do the #define after the #include. If you do it before, it will already do the substitution in the header file, which will change the enum definition into the incorrect
typedef enum {
STATE_INITIAL = 0,
STATE_LOAD = 1,
STATE_READ = 2,
2 = 3 /* or STATE_READ = 3, for the other variant; also wrong */
} state_t;
Now combining these warnings about order: you can't get that guaranteed right. If you re-#define after the #include, maybe some code inside the header files has already used STATE_FINISH, which will not have been replaced. Depending on what you want, this may be wrong.
Next, are you really sure you want to do this? Why? And don't you perhaps mean #define STATE_FINISH STATE_READ instead? The difference seems subtle, but is about expressing what you really mean.
Maybe what you mean is more like #define MY_OWN_STATE STATE_READ? If you use your own invented name for the state, you will cause less confusion about the true meaning of STATE_FINISH. It also expresses the fact that you can't change the value of STATE_FINISH as used within the library (which I hope you already realised, of course).
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