473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Pre-processor peculiarities

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
Jun 27 '08 #1
3 1183
Mamluk Caliph wrote:
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?
No. The preprocessor makes its decisions before the
compiler recognizes and processes type declarations. When
the preprocessor operates, even things like `int' and `for'
do not yet have any special significance.
* 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;
);
Ugh. "Beauty is in the eye of the beholder," but Ugh
nonetheless. Also, you'll get into trouble if anybody ever
tries something like

DEF_STRUCT( MY_FRAME,
int a, b, c;
};
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
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.
* 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.
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).

What problem are you trying to solve by using a zero-sized
type?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #2
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



Jun 27 '08 #3
Mamluk Caliph wrote:
> 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)?
It means the macro expansion is never a preprocessor
directive, not even if it looks like one. Off-hand, I cannot
think of a context where a directive-like expansion would
not be an error.
> 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?
Incomplete types, bit-fields, and functions have no size:

struct undefined *p;
void *q;
struct { int b : 4; } s;
int (*f)(double);

n = sizeof *p; /* BZZZT! */
n = sizeof *q; /* BZZZT! */
n = sizeof s.b; /* BZZZT! */
n = sizeof *f; /* BZZZT! */
> 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.
It sounds to me like you're trying to use zero-sized types
as a hack to compensate for the deficiencies of the macro.
Maybe it would be better to improve the macro than to attempt
the hack, possibly by introducing another macro for the no-data
case. (Presumably it is known which messages have no data, or
you wouldn't know which fake types to define.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
5
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
8
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.