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 7 2965
"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
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
"parenthese s."
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
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
CBFalconer <cb********@yah oo.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
Ben Pfaff wrote: CBFalconer <cb********@yah oo.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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
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.pow ernet.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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 something like
this:
1) e = (a, b, c, d); // concatenate a,b,c,d into e
2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d
For example, in the second case, assume that a,b,c,d represent 2-bit
integers, and e represents an 8-bit...
|
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, z;
The above example outputs:
|
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. because the expressions are evaluated from left to right;
|
by: benben |
last post by:
I am looking for a good example of overloading operator , (operator comma)
Any suggestions?
Ben
|
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 state\n"), TRUE) : \
(state = newstate, FALSE)
This macro is called like this:
setState(state, ST_Used);
| |
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 seems obvious to me
that comma operators would serve no purpose between function
arguments). As per C, are those commas in function argument lists the
same comma operators?
Also, are the =s used in initializations the same as any other
|
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 opposed to what is mentioned in precedence table?
Also,
with comma operator.
consider,
|
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) ? sizeof (STR) - 1 : 0)
|
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 sizeof."
I think this rule is easy to understand. Because I can find the
contexts of applying the rule as follows.
(1)
int* p = 0;
int b1 = sizeof(*p); // OK, b1 = 4, *p would not be evaluated.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |