By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

conditional macro on strings

P: n/a
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,

--
Martijn
http://www.sereneconcepts.nl
http://blogger.xs4all.nl/mihaak/
Nov 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Martijn wrote:
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.

Nov 12 '06 #2

P: n/a
"Martijn" <subscription_REMOVE_101@hot_REMOVE_mail.comwrites :
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 12 '06 #3

P: n/a
Martijn wrote:
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?

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?

--
Nick Keighley

Nov 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.