Regarding structure having members under conditional compilation

S

sam_cit

Hi Everyone,

I have a structure typedefed as

typedef strcut
{
#if(MACRO == TRUE)
int a;
int b;
#endif
} SAMPLE_STRUCT;

Now does this give compile properly? If not, i might be expected to
add another temp variable in the structure which might not be used by
my application at all, is there any workaround for this scenario?

Thanks in advance!!! ;-)
 
S

santosh

Hi Everyone,

I have a structure typedefed as

typedef strcut
{
#if(MACRO == TRUE)
int a;
int b;
#endif
} SAMPLE_STRUCT;

Now does this give compile properly?

Yes, if MACRO is TRUE.
If not, i might be expected to
add another temp variable in the structure which might not be used by
my application at all, is there any workaround for this scenario?

Yes, encapsulate the entire typedef with conditional compilation
directives.
 
I

insik.park

Hi Everyone,

I have a structure typedefed as

typedef strcut
{
#if(MACRO == TRUE)
int a;
int b;
#endif
} SAMPLE_STRUCT;

Now does this give compile properly? If not, i might be expected to
add another temp variable in the structure which might not be used by
my application at all, is there any workaround for this scenario?

Thanks in advance!!! ;-)

This is the sort of thing you can easily find out for yourself. Just
make a "test.c" file and run it through the compiler. But to answer
your question, yes, it compiles fine.
 
E

Eric Sosman

Hi Everyone,

I have a structure typedefed as

typedef strcut
{
#if(MACRO == TRUE)
int a;
int b;
#endif
} SAMPLE_STRUCT;

Now does this give compile properly?

If the compiler issues a diagnostic, it has behaved
"properly." If it doesn't, it has behaved "improperly."

If you change `strcut' to `struct', then what happens
depends on how MACRO is defined. If the #if test fails and
the `a' and `b' declarations are omitted, then the compiler
acts "properly" if and only if it emits a diagnostic.
If not, i might be expected to
add another temp variable in the structure which might not be used by
my application at all, is there any workaround for this scenario?

What do you mean by "is there any workaround?" What are
you trying to achieve? A struct containing no elements -- if
C permitted such a thing, which it doesn't -- seems a rather
useless object, not likely to help you toward any goal I can
think of off-hand.
 
R

Richard Tobin

Eric Sosman said:
A struct containing no elements -- if
C permitted such a thing, which it doesn't -- seems a rather
useless object, not likely to help you toward any goal I can
think of off-hand.

There's not much use for a struct with no members, but there are
plenty of uses for a struct that *might* have no members. For
example, a library might provide a structure having various members
depending on compile-time options, and some combination of options
might result in none being needed. Having to change function
definitions just because some particular application doesn't need any
of the optional features is just an annoyance.

-- Richard
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top