
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating and Using an Array of Pointers
|
1075
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,isa
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 remain-
ing 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.
See Also
See the “unsafe Keyword” in the ...