473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

conditional macro on strings

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
3 1685
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
1
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
28
by: richardlang | last post by:
Anyone out there ever come across a preprocessor macro that compares an argument value against the predefined __DATE__ macro in order to control conditional compilation based on date. Something...
11
by: junky_fellow | last post by:
Hi guys, I have a piece of code: int func(int flag) { int number=1; if (flag == 1) return 0; if(flag == 2)
12
by: code break | last post by:
Hi all, Please can any one tell me the importance of below code... #if 0 /*what is use of this macro conditional.since this is every time false .*/ stmt1 ; #else
3
by: WU10 | last post by:
I have built a simple database to track the mechanical condition of offshore equipment. We established a system of ranking for issues that arise and used conditional formatting of the cell's...
6
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces...
36
by: anon.asdf | last post by:
Hello! Can the proprocessor make conditional decisions. Here's an example of the functionality (not standard C!) #define PUT_BYTE(const_index, val) \ #preprocessor_if (const_index ==...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.