什么是逗号分隔的一组作业?

发布于 2024-10-06 04:20:30 字数 166 浏览 1 评论 0原文

我在例程中注意到

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舂唻埖巳落 2024-10-13 04:20:30

逗号运算符是一个序列点:每个逗号分隔的表达式从左到右进行计算。结果具有正确操作数的类型和值。从功能上讲,您的示例相当于(更具可读性?):

else
{
    *pbuf++ = '%';
    *pbuf++ = to_hex(*pstr >> 4);
    *pbuf++ = to_hex(*pstr & 15);
}

这是标准为逗号运算符提供的另一个示例(6.5.17):

在函数调用中

<前><代码>f(a, (t=3, t+2), c)

该函数有三个参数,
其中第二个值为 5。

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 ?) :

else
{
    *pbuf++ = '%';
    *pbuf++ = to_hex(*pstr >> 4);
    *pbuf++ = to_hex(*pstr & 15);
}

Here is another example that the standard provides for the comma operator (6.5.17) :

In the function call

f(a, (t=3, t+2), c)

the function has three arguments, the
second of which has the value 5.

你是暖光i 2024-10-13 04:20:30

来自维基百科:

在 C 和 C++ 编程语言中,逗号运算符(由标记 , 表示)是一个二元运算符,它计算第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)。逗号运算符的优先级是所有 C 运算符中最低的,并且充当序列点。

逗号标记作为运算符的使用不同于它在函数调用和定义、变量声明、枚举声明和类似结构中的使用,在这些结构中,逗号标记充当分隔符。

在此示例中,第二行和第三行之间的不同行为是由于逗号运算符的优先级低于赋值运算符。

int a=1, b=2, c=3, i;   // comma acts as separator in this line, not as an operator
i = (a, b);             // stores b into i                                ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;     ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a = 5 into i     ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i                                ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i                                ... a=5, b=2, c=3, i=3

链接:http://en.wikipedia.org/wiki/Comma_o​​perator

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.

int a=1, b=2, c=3, i;   // comma acts as separator in this line, not as an operator
i = (a, b);             // stores b into i                                ... a=1, b=2, c=3, i=2
i = a, b;               // stores a into i. Equivalent to (i = a), b;     ... a=1, b=2, c=3, i=1
i = (a += 2, a + b);    // increases a by 2, then stores a+b = 3+2 into i ... a=3, b=2, c=3, i=5
i = a += 2, a + b;      // increases a by 2, then stores a = 5 into i     ... a=5, b=2, c=3, i=5
i = a, b, c;            // stores a into i                                ... a=5, b=2, c=3, i=5
i = (a, b, c);          // stores c into i                                ... a=5, b=2, c=3, i=3

Link: http://en.wikipedia.org/wiki/Comma_operator

灯角 2024-10-13 04:20:30

为什么它不应该工作?它将 %to_hex(*pstr >> 4)to_hex(*pstr & 15) 设置为由pbuf。等效代码可能如下:

else {
  *pbuf = '%';
  *(pbuf + 1) = to_hex(*pstr >> 4);
  *(pbuf + 2) = to_hex(*pstr & 15);
  pbuf += 3;
}

Why it shouldn't work? It sets %, to_hex(*pstr >> 4), to_hex(*pstr & 15) to sequential memory block addressed by pbuf. The equivalent code may be the following:

else {
  *pbuf = '%';
  *(pbuf + 1) = to_hex(*pstr >> 4);
  *(pbuf + 2) = to_hex(*pstr & 15);
  pbuf += 3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文