Pre-processor peculiarities

M

Mamluk Caliph

I have an issue which I don't seem to be able to solve, and which has
haunted me from time to time for years. Solution to either of the
points below would help me immensely (the compiler I have is a plain C-
compiler without C++):

* Is there any way to determine is a 'typedef' is defined during
compile time without depending on if a corresponding HAS_xxx_t (or
something similar) has been defined with #define?

* Is there any way to #define something inside a macro? For example,
the following works:

#define DEF_STRUCT( NAME, STRUCTURE ) \
typedef struct{ \
STRUCTURE \
}frame_##NAME##_t;

From within the code I can then define the structure like this:

DEF_STRUCT( MY_FRAME,
int32_t foo:24;
int32_t bar:24;
);

However if I add an extra line last in the macro defining if the
structure is existent or not, the compiler complains:

#define DEF_STRUCT( NAME, STRUCTURE ) \
typedef struct{ \
STRUCTURE \
}frame_##NAME##_t; \
#define HAS_##NAME


* Is there any way of defining something that when combined with the
sizeof operator would result in 0?

Defining an empty structure apparently isn't legal and can't be used
as a type resulting in a zero size.


Many thanks
/Michael
 
M

Mamluk Caliph

This will not work and cannot be made to work. Even if a
macro expands to something that looks like a preprocessor
directive, the expansion is not processed as a directive.

Would this them mean that no processor directive is ever legal inside
a macro (i.e. anything with a number sign not meant for
stringification of concatenation)?

If by "combined with" you mean "used as the operand of,"
the answer is No. Every C type either has a size of at least
one byte, or has no size at all (which is not the same thing
as having a size of zero bytes).

Yes, as in "used as the operand of". How can you define something as
having no size? I'm thinking of something like the following perhaps
could be used to solve my problem:

if (sizeof(nosize_t) == 0)
fo();
What problem are you trying to solve by using a zero-sized
type?


I'm trying to re-use a macro looking like this:

#define MSG_INIT( VAR , CMD) \
DEF_MSG_SIMPLE( VAR ,HEADERID_##CMD, sizeof(frame_##CMD##_t) )

This is used for initializing a message header in a field-bus based
system with many hundred unique ID.s. The problem is that sometimes
messages have no data (i.e. there exists no frame_XXX_t). The macro
could be expanded to take the size as an argument as well, but then if
the data format changes, one would have to search and replace in
application code.

Defining anything that could be usable inside the macro, but that at
the same time wouldn't imply changes to the application code would
work. I.e. I was thinking of defining "dummy" types for those messages
missing data returning the size zero, but I can't figure out how.

Regards
/Michael
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top