If you need to do this in a function because you want to use arbitrary sized arrays, the number of elements in the array must be passed to the function:
Code:
// return the position with the largest value
// or -1 if elems == 0.
unsigned int find_largest_int( int *p, unsigned int elems )
{
unsigned int i, j;
if ( ! elems ) return -1;
for ( j = 0, i = 1; i < elems; i++ )
if ( p[i] > p[j] )
j = i;
return j;
}
Example:
Code:
int array[] = { 1, 2, 3, 7, 5, 6 };
unsigned int largest;
largest = find_largest_int( array, sizeof array / sizeof array[0] );
// don't need to test for 'largest == (unsigned int)-1' because the array has elements...
printf( "Found @%u: %d\n", largest, array[largest] );