Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > conditional macro on strings

Reply
Thread Tools

conditional macro on strings

 
 
Martijn
Guest
Posts: n/a
 
      11-12-2006
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/


 
Reply With Quote
 
 
 
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      11-12-2006
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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-12-2006
"Martijn" <subscription_REMOVE_101@hot_REMOVE_mail.com> writes:
> 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) kst- <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.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-13-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
macro to control conditional compilation based on date richardlang@my-deja.com C Programming 28 04-16-2006 06:28 AM
Conditional compilation inside a macro Srinivas Mudireddy C Programming 20 02-27-2006 07:46 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57