Getting pointer for first entry in an array

imak picture imak · Mar 30, 2012 · Viewed 21.6k times · Source

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

Answer

Steve Wellens picture Steve Wellens · Mar 30, 2012

Try this:

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

    }
}