ima*_*mak 13 c# unsafe-pointers
我想得到数组中第一个条目的指针.这就是我尝试的方式
int[] Results = { 1, 2, 3, 4, 5 };
unsafe
{
int* FirstResult = Results[0];
}
Run Code Online (Sandbox Code Playgroud)
获取以下编译错误.任何想法如何解决它?
您只能在固定语句初始值设定项中获取未固定表达式的地址
错误消息非常清楚.您可以参考MSDN.
unsafe static void MyInsaneCode()
{
int[] Results = { 1, 2, 3, 4, 5 };
fixed (int* first = &Results[0]) { /* something */ }
}
Run Code Online (Sandbox Code Playgroud)
试试这个:
unsafe
{
fixed (int* FirstResult = &Results[0])
{
}
}
Run Code Online (Sandbox Code Playgroud)
错误代码是获得答案的神奇之处 - 搜索错误代码(在您的情况下为CS0212),并且在很多情况下您会得到建议修复的解释.
搜索:http://www.bing.com/search?q = CS0212 + msdn
结果:http: //msdn.microsoft.com/en-us/library/29ak9b70%28v=vs.90%29.aspx
页面代码:
unsafe public void mf()
{
// Null-terminated ASCII characters in an sbyte array
sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
sbyte* pAsciiUpper = &sbArr1[0]; // CS0212
// To resolve this error, delete the previous line and
// uncomment the following code:
// fixed (sbyte* pAsciiUpper = sbArr1)
// {
// String szAsciiUpper = new String(pAsciiUpper);
// }
}
Run Code Online (Sandbox Code Playgroud)