Can we verify whether a typedef has been defined or not
Suppose my program is:
typedef int MYINT;
int main()
{
MYINT x = 5;
........
do_something()
........
/* I wanna test whether MYINT is defined or not */
/* I can't use: ifdef (MYINT), since MYINT is not a macro */
........
return 0;
}
Actually, I encountered this problem while I was using a cross-compiler for vxworks. The cross-compiler header file included: typedef int INT.
But, my stack's header file used:
#ifndef INT
#define int INT
Can you please suggest how to test typedefs, whether they are defined previously or not?
Thanks开发者_StackOverflow社区 in advance.
I don't think you can.
typedef
is just a typename alias for the compiler.
No, this is not possible. Your best option is to use typedef
yourself, since redefinition of a typedef
is an error.
#ifdef INT
# error "No, we want to define INT!"
#endif
typedef int INT; // error if previously typedef'd
(I suppose you didn't really want to #define int INT
:)
You can't do this with a typedef because C doesn't have any reflection capability.
All I can suggest is that you use #define as suggested by @larsmans.
#define MYINT int
...
#ifdef MYINT
// MYINT is defined
#endif
No, in C you cannot do this directly. However you can use #ifdef
to check whether a name is already defined or not.
精彩评论