www.gibmonks.com

### Recipe 19.5. Returning a Pointer to a Particular Element in an Array

#### Problem

You need to create a method that accepts a pointer to an array, searches that array for a particular element, and returns to the location of the found element in the array.

#### Solution

The FindInArray method, used in the following example, will return the position of the element found in the array given a fixed integer pointer. To see FindInFixedArray in action, look at the TestFind method shown here:

```	public void TestFind( )
{
unsafe
{
int[] numericArr = new int[3] {2,4,6};
fixed(int* ptrArr = numericArr)
{
int foundPos = FindInFixedArray(ptrArr, numericArr.Length, 4);
if (foundPos > -1)
{
Console.WriteLine("Position in array: " + foundPos);
}
else
{
}
}
}
}
```

The TestFind method creates an array of integers (numericArr), then uses the fixed statement to create a pointer (ptrArr) and to make sure the array will not be moved in memory by the garbage collector. The ptrArr pointer variable is passed to the FindInFixedArray method shown here to get the position of the element for the value being searched for:

```	public unsafe int FindInFixedArray(int* theArray, int arrayLength,
int valueToFind)
{
for (int counter = 0; counter < arrayLength; counter++)
{
if (theArray[counter] == valueToFind)
{
return (counter);
}
}
// Return -1 if the value is not found in the array.
return (-1);
}
```

Notice that the FindInFixedArray method requires that the int* be fixed to be effective. If the pointer passed in to the FindInFixedArray method is not fixed, the garbage collector could move the array being searched at any time. To avoid this, use the fixed statement as shown in the TestFind example. This method is strongly typed for arrays that contain integers. To modify this method to use another type, change the int* types to the pointer type of your choice. Note that if no elements are found in the array, a value of -1 is returned.

#### Discussion

The FindInArray method accepts three parameters. The first parameter, theArray, is a pointer to the first element in the array that will be searched. The second parameter, arrayLength, is the length of the array, and the final parameter, valueToFind, is the value you wish to find in the array theArray.

The second parameter, arrayLength, informs the for loop of the length of the array. You cannot determine the length of an array from just a pointer to that array, so this parameter is needed. Many unmanaged APIs that accept a pointer to an array also require that the length of the array be passed.

 You could pass a pointer to any element in the array through the theArray parameter, but if you do so, you must calculate the remaining length by subtracting the element location from the length of the array and passing the result to the arrayLength parameter.

The loop iterates over each element in the array and looks for the element that has a value equal to the parameter valueToFind. Once this element is found, the location of this element in the array is returned to the caller.