Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting pointer for first entry in an array

I want to get pointer of first entry in the array. This is how I tried

int[] Results = { 1, 2, 3, 4, 5 };

unsafe
{
    int* FirstResult = Results[0];
}

Get following compilation error. Any ideas how to fix it?

You can only take the address of an unfixed expression inside of a fixed statement initializer

like image 290
imak Avatar asked Mar 30 '12 16:03

imak


2 Answers

Try this:

unsafe
{
    fixed (int* FirstResult = &Results[0])
    {

    }
}
like image 116
Steve Wellens Avatar answered Oct 17 '22 07:10

Steve Wellens


The error codes are magic to get the answer - search for error code (CS0212 in your case) and you get explanation with proposed fix in a lot of case.

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 the 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);
      // }
   }
like image 29
Alexei Levenkov Avatar answered Oct 17 '22 09:10

Alexei Levenkov