C 代码(带逗号运算符)到 Delphi

发布于 2025-01-04 06:26:20 字数 336 浏览 2 评论 0原文

C 语言中以下内容的等效 Delphi 代码是什么:

int32 *P;
int32 c0, c1, i, t;
uint8 *X;

t = P[i], c0 = X[t], c1 = X[t + 1];

坦率地说,逗号运算符让我感到困惑。下面的说法是不是大错特错了?

{$POINTERMATH ON}
var P: ^Int32; c0, c1, i, t: Int32; X: ^UInt8;

t:= P[i];   //<--?
c0:= X[t];
c1:= X[t+1];
t:= c1;     //<--?

What is the equivalent Delphi code for the following in C:

int32 *P;
int32 c0, c1, i, t;
uint8 *X;

t = P[i], c0 = X[t], c1 = X[t + 1];

Frankly, the comma operator confuses me. Is the following wildly wrong?

{$POINTERMATH ON}
var P: ^Int32; c0, c1, i, t: Int32; X: ^UInt8;

t:= P[i];   //<--?
c0:= X[t];
c1:= X[t+1];
t:= c1;     //<--?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

み格子的夏天 2025-01-11 06:26:20

C 中的逗号运算符具有最低的优先级。所以你的陈述相当于:

(t = P[i]), (c0 = X[t]), (c1 = X[t + 1]);

然后从左到右评估。所以它相当于:

t = P[i];
c0 = X[t];
c1 = X[t + 1];

但是,如果您做了这样的事情:

z = (a = b, c = d);

那么它将相当于:

a = b;
c = d;
z = c;

因为逗号运算符“返回”其最终操作数。

我还应该指出,因为每个逗号都是一个序列点,所以像这样的东西是明确定义的:

i = i + 1, i++, --i;

而这不是:

i = i + i++ - --i;


It almost goes without saying: if anyone wrote production C code like your first code snippet, I would have to spank them.

The comma operator in C has the lowest possible precedence. So your statement is equivalent to:

(t = P[i]), (c0 = X[t]), (c1 = X[t + 1]);

which is then evaluated from left to right. So it's equivalent to:

t = P[i];
c0 = X[t];
c1 = X[t + 1];

However, if you had done something like this:

z = (a = b, c = d);

then it would be equivalent to this:

a = b;
c = d;
z = c;

because the comma operator "returns" its final operand.

I should also point out that because each comma is a sequence point, stuff like this is well-defined:

i = i + 1, i++, --i;

where as this isn't:

i = i + i++ - --i;


It almost goes without saying: if anyone wrote production C code like your first code snippet, I would have to spank them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文