Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comma operator

Reply
Thread Tools

Comma operator

 
 
grid
Guest
Posts: n/a
 
      06-08-2005
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
 
Reply With Quote
 
 
 
 
S.Tobias
Guest
Posts: n/a
 
      06-08-2005
grid <(E-Mail Removed)> wrote:

> 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... */

--
Stan Tobias
mailx `echo http://www.velocityreviews.com/forums/(E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
 
 
 
Zoran Cutura
Guest
Posts: n/a
 
      06-08-2005
grid <(E-Mail Removed)> wrote:
> 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.

--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
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
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM
the whys of the comma operator Michael Scarlett C Programming 8 06-19-2004 12:08 AM
Confused about the comma operator Derek C++ 6 04-13-2004 09:03 PM
Yet another comma operator question Koster C Programming 7 01-18-2004 01:07 AM
Overloading comma to give a concatenation operator: various problems Paul Davis C++ 7 07-17-2003 07:25 AM



Advertisments