Write a function and pass in an int-type array as a parameter. Use the pointer to return two results: maximum value and minimum value, and int array.
This afternoon I studied the pointer problem in C language. The core of C language is pointer, the core of pointer is address, and the core of address is memory.
#include <stdio.h>
void hanshu (int * arry, int size, int * m, int * n)
{
* m = arry [0];
* n = arry [1];
for (int i = 0; i <size; i ++)
{
if (arry [i]> * m)
* m = arry [i];
if (arry [i] <* n)
* n = arry [i];
}
}
int main (int argc, const char * argv []) {.
int a [] = {1,2,3,4,5,6}; // Define an array of six data
int max;
int min; // Storage minimum
hanshu (a, 6, & max, & min); // The function transfers the array and transfers the maximum and minimum addresses
printf ("The maxnumber is% d \ n", max);
printf ("The minnumber is% d \ n", min);
return 0;
}
The main function defines an array and defines a max and a min to save the maximum and minimum values in the array.
The values passed in the hanshu function include the first address of the array, the length of the array, and the maximum and minimum addresses. In void hanshu, first assign an initial value to the maximum and minimum values. Use a for loop to traverse all values in the array, and assign the maximum value to * m, and the minimum value to * n. At this time, the maximum Pointer Points to the maximum address, and the minimum Pointer Points to the minimum address. Complete operation