There is a header file stack.h:
typedef char ElemType;
typedef struct{
ElemType data[MaxSize];
int top;
} SqStack;
int Push_Sq(SqStack *s, ElemType e);
int Pop_Sq(SqStack *s, ElemType *e);
int GetTop_Sq(SqStack *s, ElemType *e);
And now I have some source file including this header, I want to use int for ElemType instead of char, is it possible? What should I do?(I don’t want to modify the original header file)
Well, since you’re compiling the code, there’s nothing stopping you from editing the source file itself, or using a different source file (a slightly modified copy of the one you’ve shown), to change the underlying type to an
int. In other words, changing the first line to:You’ll need to be careful as that may break assumptions made elsewhere, but that would be the first thing I’d try.
If, as stated in the edit, you don’t want to modify the original header file, you can just include it thus:
This may work as
charis only used in that header file in thetypedefitself so the effects are localised.Keep in mind that all translation units in the current build should use that trick otherwise you may end up with some very unusual bugs.
In my opinion, you’d be better off forking the entire stack implementation to try and keep things clean, or make the stack able to handle any data type (a little more complex, but doable).