Getting a pointer to the first record in an array

I want to get the pointer of the first record in an array. This is how i tried

int[] Results = { 1, 2, 3, 4, 5 }; unsafe { int* FirstResult = Results[0]; } 

Get the following compilation error. Any ideas how to fix this?

You can only accept the address of an uncommitted expression inside the fix operator fix

+7
source share
3 answers

Error codes are magic to get an answer - find the error code (CS0212 in your case) and you will get an explanation with the proposed fix in many cases.

Search: http://www.bing.com/search?q=CS0212+msdn

Result: http://msdn.microsoft.com/en-us/library/29ak9b70%28v=vs.90%29.aspx

Code from page:

  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); // } } 
+4
source

The error message is pretty clear. You can contact MSDN .

 unsafe static void MyInsaneCode() { int[] Results = { 1, 2, 3, 4, 5 }; fixed (int* first = &Results[0]) { /* something */ } } 
+5
source

Try the following:

 unsafe { fixed (int* FirstResult = &Results[0]) { } } 
+4
source

All Articles