How to remove the type defined by "typedef"?thx

H

hercules

Hi:
In some code , I used typedef unsigned char BOOL; /*boolean */

But in other code , I must use typedef unsigned int BOOL; /* boolean
*/

How to remove the previous typedef ?


Hercules
 
B

Ben Pfaff

hercules said:
In some code , I used typedef unsigned char BOOL; /*boolean */

But in other code , I must use typedef unsigned int BOOL; /* boolean
*/

How to remove the previous typedef ?

This cannot be done. However, you can use
#define BOOL unsigned char
instead of the initial typedef, then write
#undef BOOL
#define BOOL unsigned int
later to change it. Not elegant, but it should work.
 
S

Scott Fluhrer

hercules said:
Hi:
In some code , I used typedef unsigned char BOOL; /*boolean */

But in other code , I must use typedef unsigned int BOOL; /* boolean
*/

How to remove the previous typedef ?

You really can't remove or redefine a typedef[1]. About the best you can do
is patch around it with the CPP:

typedef unsigned char BOOL;
/* Some code here */

#define BOOL anotherBOOLtype
typedef unsigned int BOOL;

BTW: if it is all your code, why do you have to have the two distinct
definitions? Or, are you doing something like including two different
header files with the above definitions?

[1] Unless, of course, it was defined within a block. In that case, ending
the block will end the scope of the typedef, effectively removing it. I
rather doubt this applies to your code -- I just add this to make my
statement technically correct.
 
M

Mark McIntyre

Hi:
In some code , I used typedef unsigned char BOOL; /*boolean */

But in other code , I must use typedef unsigned int BOOL; /* boolean
*/

How to remove the previous typedef ?

You can't: Instead, fix your code. Two different defnitions of the
same type name is an accident waiting to happen.
 
E

EventHelix.com

You cannot undefine a type. If it is possible to change the
typedefs to #defines, you could use #undef to define the old
one.

However, I would not recommend it. It is probably better to make
code changes so that you do not have to live with these confusing
declarations.

Sandeep
 
D

Dan Pop

In said:
In some code , I used typedef unsigned char BOOL; /*boolean */

But in other code , I must use typedef unsigned int BOOL; /* boolean
*/

How to remove the previous typedef ?

The only legitimate situation where this can happen is when using two
libraries, each of them coming with its own definition of BOOL.

The right thing in such a case is to use different identifiers:

/* The typedef's must only be provided if the library headers */
/* expect BOOL to be already defined, instead of defining it */
/* themselves. */

typedef unsigned char lib1BOOL;
typedef unsigned int lib2BOOL;

#define BOOL lib1BOOL
#include <lib1.h>

#define BOOL lib2BOOL
#include <lib2.h>

#undef BOOL

/* use lib1BOOL and lib2BOOL in your own declarations */

Dan
 
K

Kevin Bracey

The only legitimate situation where this can happen is when using two
libraries, each of them coming with its own definition of BOOL.

The right thing in such a case is to use different identifiers:

/* The typedef's must only be provided if the library headers */
/* expect BOOL to be already defined, instead of defining it */
/* themselves. */

typedef unsigned char lib1BOOL;
typedef unsigned int lib2BOOL;

#define BOOL lib1BOOL
#include <lib1.h>

#undef BOOL

is required here; one is not allowed to redefine a currently-defined
identifier with a different definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top