无法将索引应用于“IntPtr”类型的表达式== IntPtr ptr1 = [...] - > PTR1 [0]

时间:2011-04-12 06:09:53

标签: c# indexing intptr

我找到了一个我要实现的代码片段。现在是一个函数没有运行的问题。无法将索引应用于“IntPtr”类型的表达式

        fixed (byte* numRef = this.tribuf)
        {
            for (int i = 0; i < num; i++)
            {
                item = this.trihash.GetItem(ch + S.Substring(i, 3));
                if (item != null)
                {
                    this.TrigramChecked += item.Count;
                    foreach (int num3 in item)
                    {
                        if ((num3 != id) && (numRef[num3] < 0xff))
                        {
                            IntPtr ptr1 = (IntPtr) (numRef + num3);
                            /* ToDo: Error */
                            ptr1[0] = (IntPtr) ((byte) (ptr1[0] + 1));
                        }
                    }
                }
            }
        }

问候克里斯

1 个答案:

答案 0 :(得分:2)

正如我在评论中所说,我会首先尝试避免使用不安全的代码,但它看起来就像它真的只是试图做的那样:

if ((num3 != id) && (numRef[num3] < 0xff))
{
    numRef[num3]++;
}

或者更高效(仅从numRef[num3]读取一次):

if (num3 != id)
{
    byte value = numRef[num3];
    if (value < 0xff)
    {
        numRef[num3] = (byte) (value + 1);
    }
}
相关问题