Yet another comma operator question

K

Koster

Hi folks,

As I understand it, amongst other things, the comma operator may be used to
cause any number of expressions to be evaluated (who's results are thrown
away except the last) where only one is expected, without the use of braces.

So
if (condition) i = 0, j = 1;
causes both i = 0 and j = 1 expressions to be evaluated if condition
evaluates to true. So far just making sure my understanding of the comma
operator isn't flawed.

Now, I have a function which accepts two pointers as parameters. They need
to be checked to see that they do not equal NULL. If either equals NULL, a
log entry should be made and the function should return:
void list_add(struct list * lst, void * data)
{
if (!lst) log("BUG: list_add received NULL list"), return;
if (!data) log("BUG: list_add received NULL data"), return;
...
}
My compiler tells me I have syntaxs errors on these two lines. If I were to
use braces these two lines would expand to ten lines. I'd prefer to keep
them tidy as two lines as not to distract the reader from the _functional_
parts of the function, if you know what I mean. So why doesn't this work?
The log call should be evaluated and executed, whose value is discarded, and
then the return is evaluated and executed, or not? Is the problem that the
comma operator requires the instruction pointer (or whatever you call it
when talking about C flow) to return to the place after the 2nd sequence
point in order for the operator to yield the value of the 2nd expression,
which would not be possible after a return?

Thanks for the help,
Koster
 
A

Arthur J. O'Dwyer

As I understand it, amongst other things, the comma operator may be used to
cause any number of expressions to be evaluated (who's results are thrown
away except the last) where only one is expected, without the use of braces.

Roughly correct, if you replace the word "braces" with the word
"parentheses."
So
if (condition) i = 0, j = 1;
causes both i = 0 and j = 1 expressions to be evaluated if condition
evaluates to true. So far just making sure my understanding of the comma
operator isn't flawed.

It is, but what you've *said* is correct. ;-)
Now, I have a function which accepts two pointers as parameters. They need
to be checked to see that they do not equal NULL. If either equals NULL, a
log entry should be made and the function should return:
void list_add(struct list * lst, void * data)
{
if (!lst) log("BUG: list_add received NULL list"), return;
if (!data) log("BUG: list_add received NULL data"), return;
...
}
My compiler tells me I have syntaxs errors on these two lines.

Right. The comma operator connects two *expressions*, exactly as
you wrote above. 'return' is not an expression. Q.E.D.: it doesn't
work.
If I were to
use braces these two lines would expand to ten lines.

Don't be silly! Adding two pair of braces would expand the code
by four characters; possibly more if you felt obligated to add some
whitespace. There's no requirement that you put each statement
on a separate line. Personally, I would write

void list_add(struct list *list, void *data)
{
if (list == NULL) {
log("BUG: list_add received NULL list");
return;
}
if (data == NULL) {
log("BUG: list_add received NULL data");
return;
}
...
}

....or, more probably, leave out the checks altogether in favor
of client-side checks. :) But if, as it seems, you're not concerned
with legibility, you could just as well write

void list_add(struct list *list, void *data)
{
if (list == NULL) { log("BUG: list_add received NULL list"); return; }
if (data == NULL) { log("BUG: list_add received NULL data"); return; }
...
}

I'd prefer to keep
them tidy as two lines as not to distract the reader from the _functional_
parts of the function, if you know what I mean. So why doesn't this work?

'return' is not an expression. It is a keyword which can be used
as a statement of the form

return ;

or the form

return some_expression ;

but 'return' is *not* an expression, any more than 'if' or 'for' is.

HTH,
-Arthur
 
S

Scott Fluhrer

Koster said:
Hi folks,

As I understand it, amongst other things, the comma operator may be used to
cause any number of expressions to be evaluated (who's results are thrown
away except the last) where only one is expected, without the use of braces.

So
if (condition) i = 0, j = 1;
causes both i = 0 and j = 1 expressions to be evaluated if condition
evaluates to true. So far just making sure my understanding of the comma
operator isn't flawed.

Now, I have a function which accepts two pointers as parameters. They need
to be checked to see that they do not equal NULL. If either equals NULL, a
log entry should be made and the function should return:
void list_add(struct list * lst, void * data)
{
if (!lst) log("BUG: list_add received NULL list"), return;
if (!data) log("BUG: list_add received NULL data"), return;
...
}
My compiler tells me I have syntaxs errors on these two lines. If I were to
use braces these two lines would expand to ten lines. I'd prefer to keep
them tidy as two lines as not to distract the reader from the _functional_
parts of the function, if you know what I mean. So why doesn't this work?
The log call should be evaluated and executed, whose value is discarded, and
then the return is evaluated and executed, or not? Is the problem that the
comma operator requires the instruction pointer (or whatever you call it
when talking about C flow) to return to the place after the 2nd sequence
point in order for the operator to yield the value of the 2nd expression,
which would not be possible after a return?
No. The problem is that the comma operator expects expressions on both
sides, and 'return' is not an expression, it is a statement. It makes as
little sense to the compiler as would:
3 + return 5;


If you're worried about the line count, how about:

void list_add(struct list * lst, void * data)
{
if (!lst) { log("BUG: list_add received NULL list"); return; }
if (!data) { log("BUG: list_add received NULL data"); return; }
...
}
 
C

CBFalconer

Koster said:
.... snip ...

Now, I have a function which accepts two pointers as parameters.
They need to be checked to see that they do not equal NULL. If
either equals NULL, a log entry should be made and the function
should return:
void list_add(struct list * lst, void * data)
{
if (!lst) log("BUG: list_add received NULL list"), return;
if (!data) log("BUG: list_add received NULL data"), return;
...
}

Arthur has discussed the actual syntax. I suggest the following
for vertical compactness and clarity:

void list_add(struct list *lst, void *data)
{
if (!lst) log("BUG: list_add received NULL list");
else if (!data) log("BUG: list_add received NULL data");
else {
....
}
return;
}
 
B

Ben Pfaff

CBFalconer said:
Arthur has discussed the actual syntax. I suggest the following
for vertical compactness and clarity:

void list_add(struct list *lst, void *data)
{
if (!lst) log("BUG: list_add received NULL list");
else if (!data) log("BUG: list_add received NULL data");
else {
....
}
return;
}

How does an empty return as the last statement in a function
clarify anything? Furthermore, how does it make the function
more vertically compact?
 
C

CBFalconer

Ben said:
How does an empty return as the last statement in a function
clarify anything? Furthermore, how does it make the function
more vertically compact?

ee-yup. Not exactly necessary. But absence might confuse the OP.
 
R

Richard Heathfield

Koster said:
Now, I have a function which accepts two pointers as parameters. They
need
to be checked to see that they do not equal NULL. If either equals NULL,
a log entry should be made and the function should return:
void list_add(struct list * lst, void * data)
{
if (!lst) log("BUG: list_add received NULL list"), return;

Others have answered your specific question; I just wanted to add that log()
is a mathematical function declared in <math.h>, so you might want to
choose a different name for your logging function.
 
O

Old Wolf

if (!lst) log("BUG: list_add received NULL list"), return;

As well as what others have suggested:
return log("blah"), 1;

if, of course, it suits you to return something from log()
and from the current function
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top