Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do you check if the type (typedef) has already been defined??

477 views
Skip to first unread message

Mike McCarty

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

You can't, unless your compiler provides specific extensions.

You *can* however do this

#ifndef SPECIAL_TYPES_DEFINED
#include "spectyp.h"
#endif

This include file can then define your own special types.

I usually put this kind of checking *inside* the file so that "multiple
inclusion" does not hurt, and then always include the file at the
beginning:

(contents of spectyp.h)

#ifndef SPECTYP_H
#define SPECTYP_H

/* get size_t */
#include <stdlib.h>

typedef void **PPVOID;
...

#endif

(other files.c)

#include <stdio.h> (or whatever)
#include "spectyp.h"
...

In article <clcm-1998...@plethora.net>,
Rok Papez <nospam.R...@kiss.uni-lj.si> wrote:
)Hello.
)
)I'm working on a multi-platform "stuff" and have some problems :).
)For my code I need different types defined.
)
)So I have done the following:
)
)#ifndef size_t
)typedef unsigned int size_t;
)#endif
)...
)...
)...
)#ifndef PPVOID
)typedef void **PPVOID;
)#endif
)
)However #ifndef dosn't catch defined types,
)so if the following types are defined in the system
)headers I get a bunch of warnings...
)
)How do you check if the type has already been defined??
)
)
)
)--
)comp.lang.c.moderated - cl...@plethora.net


--
----
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
I don't speak for DSC. <- They make me say that.
--
comp.lang.c.moderated - cl...@plethora.net

Lawrence Kirby

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

In article <clcm-1998...@plethora.net>
nospam.R...@kiss.uni-lj.si "Rok Papez" writes:

>Hello.


>
>I'm working on a multi-platform "stuff" and have some problems :).

>For my code I need different types defined.
>

>So I have done the following:
>

>#ifndef size_t
>typedef unsigned int size_t;
>#endif
>...
>...
>...
>#ifndef PPVOID
>typedef void **PPVOID;
>#endif


>
>However #ifndef dosn't catch defined types,

>so if the following types are defined in the system

>headers I get a bunch of warnings...
>

>How do you check if the type has already been defined??

Unfortunately C doesn't provide any way to do this? However do you really
need to support platforms as non-standard as this? Any hosted C compiler
should define size_t in <stddef.h> as well as various other headers.
It would be a particularly obvious omission from <stddef.h> since there
isn't a great deal in it. For the second one I guess you could use
something like:

#ifndef PPVOID
#define PPVOID PPVOID
typedef void **PPVOID;
#endif

although that is something that should perhaps go in a header which you
could protect in the usual way. (Although t isn't instantly clear to me
why you would want something like this in the first place).

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------
--
comp.lang.c.moderated - cl...@plethora.net

Jack Klein

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

On Tue, 16 Jun 1998 21:52:57 GMT, Rok Papez
<nospam.R...@kiss.uni-lj.si> wrote:

> Hello.
>
> I'm working on a multi-platform "stuff" and have some problems :).
> For my code I need different types defined.
>
> So I have done the following:
>
> #ifndef size_t
> typedef unsigned int size_t;
> #endif

If the compiler does not provide size_t it is broken.

> #ifndef PPVOID
> typedef void **PPVOID;
> #endif
>
> However #ifndef dosn't catch defined types,
> so if the following types are defined in the system
> headers I get a bunch of warnings...
>
> How do you check if the type has already been defined??

<Jack>

For user defined types (like PPVOID), you can guard like this:

#ifndef PPVOID_DEFINED
#define PPVOID_DEFINED
typedef void **PPVOID;
#endif

</Jack>
--
comp.lang.c.moderated - cl...@plethora.net

Douglas A. Gwyn

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

Rok Papez wrote:
> #ifndef size_t
> typedef unsigned int size_t;
> #endif

That doesn't work because preprocessing (#ifndef) is performed
by the C translator at an earlier stage ("phase of translation")
than typedef processing. The usual trick is:

#ifndef size_t_DEFINED
#define size_t_DEFINED


typedef unsigned int size_t;
#endif

--
comp.lang.c.moderated - cl...@plethora.net

David Ferguson

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

>#ifndef PPVOID
>typedef void **PPVOID;
>#endif
>
>However #ifndef dosn't catch defined types,
>so if the following types are defined in the system
>headers I get a bunch of warnings...
>
>How do you check if the type has already been defined??

You can't check for previous typedef's. #if[n]def can only check for
#defined macros. However, it is OK to have multiple typedef'initions as
long as they all resolve to the same type. Simple choose type names that do
not conflict with existing names in use.

Cheers...David


--
comp.lang.c.moderated - cl...@plethora.net

A.van.Kessel

unread,
Jun 20, 1998, 3:00:00 AM6/20/98
to

(agreeing with other posters: you cannot, if your compiler does not
understand size_t, etc, bla, bla)

The way to check for it is to use the typedef'd type and attempt
to compile. Some configure scripts automate this process by creating
a small test.c file and compiling it. dependant on the compilers exitcode
proper action is taken, eg writing a line like
#define NEED_SOMETYPE 1
to a config.h file.

If you are on unix, you are lucky...


For simple cases (not too many configurations) it could be easier to just
handcode the conditional compiles/includes , based on some PLATFORM-#define.

--
Happy hacking,

Adriaan van Kessel.
Ingres DBA, C/Unix hacker
Email: Adriaan.v...@NotThere.rivm.nl
(remove NotThere. from the above address)
*** Nederlandstalige zachtwaar is een pijn in de aars ***
--
comp.lang.c.moderated - cl...@plethora.net

Andreas Schwab

unread,
Jun 20, 1998, 3:00:00 AM6/20/98
to

"Douglas A. Gwyn" <dag...@home.com> writes:

|> Rok Papez wrote:
|> > #ifndef size_t
|> > typedef unsigned int size_t;
|> > #endif
|>
|> That doesn't work because preprocessing (#ifndef) is performed
|> by the C translator at an earlier stage ("phase of translation")
|> than typedef processing. The usual trick is:
|>
|> #ifndef size_t_DEFINED
|> #define size_t_DEFINED
|> typedef unsigned int size_t;
|> #endif

Or use this:

#ifndef size_t
typedef unsigned int size_t;

#define size_t size_t
#endif

--
Andreas Schwab "And now for something
sch...@issan.informatik.uni-dortmund.de completely different"
sch...@gnu.org
--
comp.lang.c.moderated - cl...@plethora.net

0 new messages