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
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