【发布时间】:2021-10-31 08:21:03
【问题描述】:
目前我正在尝试将一些代码从 C 转换为 C#,但遇到了一些问题。 因为我是 C# 程序员并且不知道 C 我必须在这里问一些东西(没有找到任何可以回答我的问题的东西)。我在 C 中有以下代码:
struct item { short sectorno,sx1,sx2; } queue[MaxQueue], *head=queue, *tail=queue;
short ytop[W]={0}, ybottom[W], renderedsectors[NumSectors];
*head = (struct item) { player.sector, 0, W-1 };
if(++head == queue+MaxQueue) head = queue;
const struct item now = *tail;
if(++tail == queue+MaxQueue) tail = queue;
if(renderedsectors[now.sectorno] & 0x21) continue;
++renderedsectors[now.sectorno];
我是用 C# 写的:
Item[] queue = new Item[MaxQueue];
Item[] head = queue;
Item[] tail = queue;
int[] ytop = new int[W]; //{ 0 };
int[] ybottom = new int[W];
int[] renderedsectors = new int[NumSectors];
for (int x = 0; x < W; ++x)
{
ybottom[x] = H - 1;
}
for (int n = 0; n < NumSectors; ++n)
{
renderedsectors[n] = 0;
}
head[0] = new Item { sectorno = player.sector, sx1 = 0, sx2 = W - 1 };
if (head[1] == queue[queue.Length - 1])
{
head = queue;
}
我希望到目前为止这是正确的。现在我的问题:
做什么
short ytop[W]={0}
做吗? 这是一系列的短裤,对吧?这里是把0分配给数组的第一个位置吗?这就是我从调试器中得出的结论。
下面这行我看不懂:
if(++head == queue+MaxQueue) head = queue;
数组上的 ++ 是否会增加数组的索引? queue+32 是否意味着数组的最后一个位置? (奇怪,因为那将是位置 32,这将超出范围......)
最后一件事..这是什么意思?
++renderedsectors[now.sectorno];
我会说增加了数组的索引,但是它在哪里以及如何存储?
非常感谢!如果能帮助我理解这段 C 代码,我会很高兴的。
【问题讨论】:
-
“我希望到目前为止这是正确的” - 它不是。如果您想了解 C 代码,则需要了解指针。或者您可以重写它以使用数组中的索引(这就是您在 C# 中处理它的方式)
-
我在某处读到 C# 中不需要指针。你能用 C 重写这段代码来告诉我你的意思吗?我的意思是索引的解决方案
标签: c# arrays c increment translate