Comma operator

G

grid

Hi,
I need some clarifications on how the comma operator is used to return
values from function like macros.I saw a typical implementation as :

#define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )

How is the 0 after the comma operator used to return 0 on success of the
macro function ?

TIA
 
S

S.Tobias

grid said:
I need some clarifications on how the comma operator is used to return
values from function like macros.I saw a typical implementation as :
#define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )
How is the 0 after the comma operator used to return 0 on success of the
macro function ?

`sigfillset' is not a function, it's a "function-like macro", but this
detail is not very important here. A macro does not return anything,
it gets expanded. In the above case, it gets expanded into a comma
expression, whose value is _always_ 0, regardless whether the first
operand expression succeeded or not.

The above code may be some sort of optimization (macro expansion
in place of function call). `sigfillset' is probably one of many
"functions", which return 0 on success. Since setting a bit
pattern cannot ever fail, we can always "return" 0.

Example:
int error;
error = sigfillset(/*...*/);
if (error) abort();
error = sigfooset(/*...*/);
if (error) abort();
error = sigbarset(/*...*/);
if (error) abort();
/* etc... */
 
Z

Zoran Cutura

grid said:
Hi,
I need some clarifications on how the comma operator is used to return
values from function like macros.I saw a typical implementation as :

#define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 , 0 )

How is the 0 after the comma operator used to return 0 on success of the
macro function ?

Actually it allways returns 0. The point is that the comma-operator is
of such low precedence that the left operand of it is evaluated first,
so in the above case the assignement to *ptr is done and thereafter the
evaluation of the right hand operator of the ',' is done and becomes the
recult of the expression. So if it is meant to return 0 on success one
could rely on sigfillset to never fail.

OTOH, if ptr is not a valid pointer anything might happen.
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top