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

Yet another comma operator question

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
Nov 14 '05 #1
7 2939

"Koster" <re************@usenet.tld> wrote in message
news:pb******************************@40tude.net.. .
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; }
...
}

--
poncho
Nov 14 '05 #2

On Fri, 16 Jan 2004, Koster wrote:

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

Nov 14 '05 #3
Koster wrote:
.... 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;
}

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
CBFalconer <cb********@yahoo.com> writes:
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?
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 14 '05 #5
Ben Pfaff wrote:
CBFalconer <cb********@yahoo.com> writes:
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?


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
Koster wrote:
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.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #7
> 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
Nov 14 '05 #8

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
8
by: Bo Sun | last post by:
hi, suppose I have the following expression (all variables are of integer type) result = (first = 2, second = first + 1, third = second + 1); what is the value of result? 1) result is 4....
2
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
6
by: pedroalves | last post by:
Hi all, This is not a question about how to #define COMMA , Please keep reading. Recently in binutils, we introduced a macro like this: #define STRING_COMMA_LEN(STR) \ (STR), ((STR) ?...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.