conditional macro on strings

M

Martijn

Hi,

I hope I am in the right place on preprocessor help, 'cause the GNU
preprocessor documentation is somewhat minimal on this topic.

Assume I have two #define'd constants:

#define STRING1 "string"
#define STRING2 NULL

The code I want to compile is something like this:

char *sz1 = NULL;
char *sz2 = NULL;

if ( STRING1 )
sz1 = strdup(STRING1);
if ( STRING2 )
sz2 = strdup(STRING2);

(traded style for brevity)
I tried a conditional micro at first ( #if STRING1 ), but that makes the
preprocessor choke. If I use the construct above, I get a warning for each
time I use it (about strdup requiring a non-NULL argument).

Is there a better alternative than what I am presenting here?

Thanks for your time and help,
 
G

Guest

Martijn said:
Hi,

I hope I am in the right place on preprocessor help, 'cause the GNU
preprocessor documentation is somewhat minimal on this topic.

Assume I have two #define'd constants:

#define STRING1 "string"
#define STRING2 NULL

The code I want to compile is something like this:

char *sz1 = NULL;
char *sz2 = NULL;

if ( STRING1 )
sz1 = strdup(STRING1);
if ( STRING2 )
sz2 = strdup(STRING2);

(traded style for brevity)
I tried a conditional micro at first ( #if STRING1 ), but that makes the
preprocessor choke. If I use the construct above, I get a warning for each
time I use it (about strdup requiring a non-NULL argument).

Is there a better alternative than what I am presenting here?

Thanks for your time and help,

Why do you need macros? You could try simply declaring them as
constants.

const char * const STRING1 = "string";
const char * const STRING2 = NULL;

For the code that you have shown, it should work just as well. If there
is a good reason for your use of macros, though, sorry, don't count on
checking during preprocessing. It may be possible, but if it is (I'm
not sure), it would require nasty preprocessor abuse.
 
K

Keith Thompson

Martijn said:
I hope I am in the right place on preprocessor help, 'cause the GNU
preprocessor documentation is somewhat minimal on this topic.

Assume I have two #define'd constants:

#define STRING1 "string"
#define STRING2 NULL

The code I want to compile is something like this:

char *sz1 = NULL;
char *sz2 = NULL;

if ( STRING1 )
sz1 = strdup(STRING1);
if ( STRING2 )
sz2 = strdup(STRING2);

(traded style for brevity)
I tried a conditional micro at first ( #if STRING1 ), but that makes the
preprocessor choke.

Right, a preprocessor #if condition knows nothing about null pointers
or string literals.
If I use the construct above, I get a warning for each
time I use it (about strdup requiring a non-NULL argument).

First, I'll mention that strdup() is not a standard C function.

A compiler is allowed to warn about anything it likes. Apparently
yours is smart enough to issue a warning for strdup(NULL), but not
smart enough to recognize that the call will never be executed because
you've already checked the value. (BTW, gcc doesn't give me that
warning; maybe you're using different options.)

It's conceivable that the warning will go away if you increase the
optimization level, causing the compiler to do more analysis, but
that's not a good solution. Ideally, you should increase the
optimization level if you want more optimization, not to get rid of
warnings.

You can probably restructure your code to eliminate the warning, but
the cure could easily be worse than the disease. There might be an
option to disable that warning, but it's a useful warning in some
cases.

A good rule of thumb is to eliminate all warnings, but it's only a
rule of thumb. There *are* times when you know better than the
compiler; this may be one of them.

Or there may be a cleaner way to write this code that also avoids the
warning, but it's hard to tell without seeing more of your code.
 
N

Nick Keighley

Martijn said:
I hope I am in the right place on preprocessor help, 'cause the GNU
preprocessor documentation is somewhat minimal on this topic.

you're in the right place

Assume I have two #define'd constants:

#define STRING1 "string"
#define STRING2 NULL

The code I want to compile is something like this:

char *sz1 = NULL;
char *sz2 = NULL;

if ( STRING1 )
sz1 = strdup(STRING1);

I don'e understand what you are trying to do. STRING is non NULL hence
it isn't false hence the test will aleays succeed.

if ( STRING2 )
sz2 = strdup(STRING2);

similarly this test will always fail.

(traded style for brevity)
I tried a conditional micro at first ( #if STRING1 ), but that makes the
preprocessor choke.

again, what did you want it ot do?
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top