Sams Teach Yourself C in 21 Days

(singke) #1
Understanding Pointers 213

9


15: for (count = 0; count < MAX; count++)
16: {
17: printf(“Enter an integer value: “);
18: scanf(“%d”, &array[count]);
19: }
20:
21: /* Call the function and display the return value. */
22: printf(“\n\nLargest value = %d\n”, largest(array, MAX));
23:
24: return 0;
25: }
26: /* Function largest() returns the largest value */
27: /* in an integer array */
28:
29: int largest(int num_array[], int length)
30: {
31: int count, biggest = -12000;
32:
33: for ( count = 0; count < length; count++)
34: {
35: if (num_array[count] > biggest)
36: biggest = num_array[count];
37: }
38:
39: return biggest;
40: }

Enter an integer value: 1
Enter an integer value: 2
Enter an integer value: 3
Enter an integer value: 4
Enter an integer value: 5
Enter an integer value: 10
Enter an integer value: 9
Enter an integer value: 8
Enter an integer value: 7
Enter an integer value: 6
Largest value = 10
The function used in this example to accept a pointer to an array is called
largest(). The function prototype is in line 9 and with the exception of the
semicolon, it is identical to the function header in line 29.
Most of what is presented in the function header in line 29 should make sense to you:
largest()is a function that returns an intto the calling program; its second argument is
anintrepresented by the parameter length. The only thing new is the first parameter,

LISTING9.4 continued

OUTPUT

ANALYSIS

15 448201x-CH09 8/13/02 11:21 AM Page 213

Free download pdf