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.

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.