c# - * 运算符必须应用于代码中的指针错误

标签 c# visual-studio pointers immediate-window

我可以在即时窗口中执行 var b = *myObject;,它会给我一个 0x123456 格式的值。但我不能在代码中做到这一点。然后它说

The * or -> operator must be applied to a pointer

为什么我可以在即时窗口中执行此操作,但不能在代码中执行?

最佳答案

使用不安全指针访问的示例 vs.编码

IntPtr pLineBuf;
int[] lineData;
//.
//.
//.

pLineBuf = Marshal.AllocHGlobal(8192 * sizeof(byte));
lineData = new int[8192];

// access elements like this
unsafe
{
    byte* pa = (byte*)plineBuf;  // cast from IntPtr
    for (int j=0; j<10; j++)
       lineData[j] = *(pa + j);    
}

// OR

for (int j=0; j<10; j++)
     lineData[j] = (Marshal.ReadByte(plineBuf + j)); 

关于c# - * 运算符必须应用于代码中的指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32519830/

相关文章:

javascript - 如何在jquery中的popup中访问Json返回值

visual-studio - 编码 UI 测试中的 TestContext TestRunParameters

visual-studio - 为什么对 NuGet 存储库使用 SSL?

c - c中(char*)是什么意思?

c# - 如何让复选框列表选择的属性为真

c# - 创建二进制文件时的文件扩展名是什么

c# - 无法通过 IP 地址连接到 SQL Server Express - c#

c# - VS 2012 中缺少实体数据模型

c - 排序功能停止工作,没有任何错误 - C

c++ - 内存中是否有地址为 NULL(0) 的物理部分?