什么是逗号分隔的一组作业?
我在例程中注意到
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
为什么它有效?
它有什么作用?
I noticed in a routine
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
Why does it work?
What does it do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
逗号运算符是一个序列点:每个逗号分隔的表达式从左到右进行计算。结果具有正确操作数的类型和值。从功能上讲,您的示例相当于(更具可读性?):
这是标准为逗号运算符提供的另一个示例(6.5.17):
A comma operator is a sequence point : each comma separated expression are evaluated from left to right. The result has the type and value of the right operand. Functionally, your example is equivalent to (the much more readable ?) :
Here is another example that the standard provides for the comma operator (6.5.17) :
来自维基百科:
在 C 和 C++ 编程语言中,逗号运算符(由标记 , 表示)是一个二元运算符,它计算第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)。逗号运算符的优先级是所有 C 运算符中最低的,并且充当序列点。
逗号标记作为运算符的使用不同于它在函数调用和定义、变量声明、枚举声明和类似结构中的使用,在这些结构中,逗号标记充当分隔符。
在此示例中,第二行和第三行之间的不同行为是由于逗号运算符的优先级低于赋值运算符。
链接:http://en.wikipedia.org/wiki/Comma_operator
From Wikipedia:
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point.
The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.
In this example, the differing behavior between the second and third lines is due to the comma operator having lower precedence than assignment.
Link: http://en.wikipedia.org/wiki/Comma_operator
为什么它不应该工作?它将
%
、to_hex(*pstr >> 4)
、to_hex(*pstr & 15)
设置为由pbuf
。等效代码可能如下:Why it shouldn't work? It sets
%
,to_hex(*pstr >> 4)
,to_hex(*pstr & 15)
to sequential memory block addressed bypbuf
. The equivalent code may be the following: