我想獲取數組中第一個條目的指針。這就是我的嘗試獲取數組中第一個條目的指針
int[] Results = { 1, 2, 3, 4, 5 };
unsafe
{
int* FirstResult = Results[0];
}
獲取以下編譯錯誤。任何想法如何解決它?
只能採取非固定式的地址 固定語句初始化
我想獲取數組中第一個條目的指針。這就是我的嘗試獲取數組中第一個條目的指針
int[] Results = { 1, 2, 3, 4, 5 };
unsafe
{
int* FirstResult = Results[0];
}
獲取以下編譯錯誤。任何想法如何解決它?
只能採取非固定式的地址 固定語句初始化
的錯誤代碼是魔術得到的答案 - (你的情況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);
// }
}
錯誤消息的內部是相當清楚的。你可以參考MSDN。
unsafe static void MyInsaneCode()
{
int[] Results = { 1, 2, 3, 4, 5 };
fixed (int* first = &Results[0]) { /* something */ }
}
試試這個:
unsafe
{
fixed (int* FirstResult = &Results[0])
{
}
}
對不起我的無知,但它不是我清楚:( – imak 2012-03-30 16:08:29
'Results'是不固定的。除非你解決它,GC可能會移動它。 – 2012-03-30 16:10:37