check structure member at compile-time

  • Thread starter Dmitri Vorobiev
  • Start date
D

Dmitri Vorobiev

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
 
E

Eric Sosman

Dmitri said:
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."
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top