473,386 Members | 1,694 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,386 software developers and data experts.

Conditional compilation inside a macro

Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Thanks,
Srinivas

Feb 25 '06 #1
20 7703

"Srinivas Mudireddy" <sr****************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.


Does it fail with line continuation character, '\' backslash, at the end of
each line, except the last?

RP
Feb 25 '06 #2
Srinivas Mudireddy wrote:

I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.


#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Feb 25 '06 #3
Srinivas Mudireddy wrote:
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Why on Earth would you want to do that? Just use an inline function.

--
Ian Collins.
Feb 25 '06 #4
CBFalconer wrote:
Srinivas Mudireddy wrote:
I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.

#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)


Or
#define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)

But still fragile, try invoking with NULL for y.

--
Ian Collins.
Feb 25 '06 #5
Ian Collins wrote:

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Why on Earth would you want to do that? Just use an inline function.

Sorry, I thought I was down the hall in the C++ group where my
pathological hatred of macros belongs...

--
Ian Collins.
Feb 25 '06 #6

Srinivas Mudireddy 写道:
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Thanks,
Srinivas


Maybe this will work:
#define SET_VAL(x, y) ( y == NULL ? (x=0) : ((x) = *(y)))

Feb 25 '06 #7
Ian Collins wrote:

CBFalconer wrote:
Srinivas Mudireddy wrote:
I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.

#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)


Or
#define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)

But still fragile, try invoking with NULL for y.


It will work if y is a null pointer object,
which I think is probably sufficient,
because I don't think it makes sense to write
SET_VAL(x, NULL) in code,
when you know that zero is the value that you want there.

--
pete
Feb 25 '06 #8
Ian Collins wrote:
CBFalconer wrote:
Srinivas Mudireddy wrote:
I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.


#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)


Or
#define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)

But still fragile, try invoking with NULL for y.


I see no difficulty with NULL.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Feb 25 '06 #9
CBFalconer wrote:

Ian Collins wrote:
CBFalconer wrote:
Srinivas Mudireddy wrote:

I am facing a problem with trying to conditionally compile inside
a macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to
0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
Is there any way I can get around this problem with still using a
macro? I can define a function to do the same, but I would prefer
to use a macro.

#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)


Or
#define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)

But still fragile, try invoking with NULL for y.


I see no difficulty with NULL.


((x) = (NULL) ? *(NULL) : 0)

The compiler won't translate the *(NULL) expression.
But as I implied elsethread, I think it's more likely
that OP was considering that y might be a null pointer lvalue,
than that OP would actually write
SET_VAL(x, NULL);
in his source code.

--
pete
Feb 25 '06 #10

"Ian Collins" <ia******@hotmail.com> wrote in message
news:11***************@drone2-svc-skyt.qsi.net.nz...
This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Why on Earth would you want to do that? Just use an inline function.

Sorry, I thought I was down the hall in the C++ group where my
pathological hatred of macros belongs...


C knows inline functions too these days :)
Feb 25 '06 #11
On 2006-02-25, Srinivas Mudireddy <sr****************@gmail.com> wrote:
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro.
No, it's not working because preprocessor directives can only expand to
one line, and they cannot expand to other preprocessor directives.
Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.


#define SET_VAL(x,y) ((x)=(y)?(*y):0)
Feb 25 '06 #12

Ian Collins wrote:
Ian Collins wrote:

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Why on Earth would you want to do that? Just use an inline function.
Thats true. I can use inline functions. Forgot about that.

Sorry, I thought I was down the hall in the C++ group where my
pathological hatred of macros belongs...

--
Ian Collins.


Feb 25 '06 #13

WaterWalk wrote:
Srinivas Mudireddy 写道:
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.

Thanks,
Srinivas


Maybe this will work:
#define SET_VAL(x, y) ( y == NULL ? (x=0) : ((x) = *(y)))


Thx for the suggestion. I thought my simplified example will be enough,
but seems like it is confusing. There are 3 subsystems involved here.
Subsystem A talks about QoS in terms of structure X. Variables x and y
are of type X. Subsystem C tells subsystem B whether QoS is granted or
not in terms of structure M. B looks at M and checks if it has a
special value which means that QoS is not granted. If it is not, it
calls SET_VAL(x, NULL). If QoS is granted, it calls SET_VAL(x, y). When
NULL is passed, I would like to memset x with 0. So ternary operation
will not work.

I apologise for giving a wrong example and throwing you guys in wrong
direction.

Thx,
Srinivas

Feb 25 '06 #14
On Sat, 25 Feb 2006 03:40:25 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote in comp.lang.c:

"Srinivas Mudireddy" <sr****************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.


Does it fail with line continuation character, '\' backslash, at the end of
each line, except the last?


If it does not "fail" with the modification you suggested, the
implementation is very severely non-conforming.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 25 '06 #15
"Srinivas Mudireddy" <sr****************@gmail.com> writes:
WaterWalk wrote:

[...]
Maybe this will work:
#define SET_VAL(x, y) ( y == NULL ? (x=0) : ((x) = *(y)))


Thx for the suggestion. I thought my simplified example will be enough,
but seems like it is confusing. There are 3 subsystems involved here.
Subsystem A talks about QoS in terms of structure X. Variables x and y
are of type X. Subsystem C tells subsystem B whether QoS is granted or
not in terms of structure M. B looks at M and checks if it has a
special value which means that QoS is not granted. If it is not, it
calls SET_VAL(x, NULL). If QoS is granted, it calls SET_VAL(x, y). When
NULL is passed, I would like to memset x with 0. So ternary operation
will not work.

I apologise for giving a wrong example and throwing you guys in wrong
direction.


Why not just write it as a function? It's going to be a lot more
flexible and legible than a macro. It makes sense to use a macro
*only* if the operation is performance-critical *and* you're concerned
about compilers that don't support inline functions.

--
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.
Feb 25 '06 #16

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:qe********************************@4ax.com...
On Sat, 25 Feb 2006 03:40:25 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote in comp.lang.c:

"Srinivas Mudireddy" <sr****************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I can define a function to do the same, but I would prefer to use a
macro.


Does it fail with line continuation character, '\' backslash, at the end of each line, except the last?


If it does not "fail" with the modification you suggested, the
implementation is very severely non-conforming.


True #if-#else-#endif. The point was to get him thinking about a solution
which CBFalconer posted outright...

RP
Feb 25 '06 #17
Do you have to guard against the scenario that y might be void*?

Feb 26 '06 #18
obdict wrote:

Do you have to guard against the scenario that y might be void*?


I dunno. It would seem to depend on the usage of y. Without any
context that is totally unknown.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Feb 26 '06 #19
[regarding defining a macro weirdly]

In article <11**********************@i39g2000cwa.googlegroups .com>
Srinivas Mudireddy <sr****************@gmail.com> wrote:
Thx for the suggestion. I thought my simplified example will be enough,
but seems like it is confusing. ... [In one case, other code the OP probably did not write]calls SET_VAL(x, NULL). [In a completely different case, that other code]calls SET_VAL(x, y). When NULL is passed, I would like to memset x
with 0. So ternary operation will not work.


It can still be made to work using a variant like the one someone
suggested, e.g.:

#define SET_VAL(x, y) \
((y) == NULL ? \
(void)memset(&(x), 0, sizeof(x)) : \
(void)((x) = *(y)))

However, this strikes me as solving the wrong problem, which can
be re-described as follows:

"Some code I did not write insists on using macro X with
particular arguments for situation A, and the same macro X with
different arguments for completely different situation B."

The question was then:

"How can I write a complicated version of macro X that
distinguishes between the two situations based on one of the
arguments?"

when the obvious "right" approach is: "Change the other code to
use two *different* macros for the two different situations."
(Given the text I snipped, possible macros might be something like
"ALLOW_QoS" and "DENY_QoS". The "DENY" macro would take only one
argument, rather than two.)

That is, fix the real problem, rather than kludging up a work-around.

(Of course, sometimes fixing the real problem is rejected; occasionally
it is even rejected for a good reason.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Feb 27 '06 #20
"Rod Pemberton" <do*********@sorry.bitbucket.cmm> writes:
"Srinivas Mudireddy" <sr****************@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro

#define SET_VAL(x, y) ((x) = *(y))

But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to

#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif

This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.


Does it fail with line continuation character, '\' backslash, at the end of
each line, except the last?


It had certainly /better/ fail, regardless of backslash.
Feb 27 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
2
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I...
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...
4
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e....
4
by: sam_cit | last post by:
Hi Everyone, I have a structure typedefed as typedef strcut { #if(MACRO == TRUE) int a; int b; #endif
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...
4
by: stmfc | last post by:
in C and C++ conditional compilation is very useful for debuging. (using debugging MACROs) java does not support conditional compilation. what can we use in java to replace MACRO usage of C/C++ ?
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.