469,077 Members | 1,383 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,077 developers. It's quick & easy.

check structure member at compile-time

Hello group,

Is it possible to check the presence of a structure member at compile
time using only the C preprocessor?

What I have in mind could look like the following.

Suppose the structures s1 and s2 look like

struct s1 {
int A;
......
int Z;
}

struct s2 {
int B;
......
int Z;
}

Then this code snippet

#if EXISTS(s1, A)
#error struct s has a member A
#else
#error struct s has no member A
#endif

needs to produce the "struct s has a member A" message, while this one

#if EXISTS(s2, A)
#error struct s has a member A
#else
#error struct s has no member A
#endif

needs to produce the "struct s has no member A".

How do I go about defining the macro EXISTS in such case? Is it at all
possible?

Thanks,

Dmitri
Nov 14 '05 #1
1 4120
Dmitri Vorobiev wrote:
Hello group,

Is it possible to check the presence of a structure member at compile
time using only the C preprocessor?


No. The preprocessor does its work on a stream of
tokens, and the recognition of tokens as `if' or `3.14'
or `*=' happens after the preprocessor is finished. In
particular, `struct' is merely a token as far as the
preprocessor is concerned; it isn't recognized as a
keyword until later.

Situations like this are the origin of the saying
"The preprocessor doesn't know C."

--
Er*********@sun.com

Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ganesh Kundapur | last post: by
6 posts views Thread by Eric Smith | last post: by
9 posts views Thread by B Vidyadhar Joshi | last post: by
7 posts views Thread by Jake Thompson | last post: by
5 posts views Thread by Steven Woody | last post: by
3 posts views Thread by Michael | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.