C-language function pointer: Gets the maximum of any type array

Source: Internet
Author: User

1#include <stdio.h>2  3 //{3,5,8,7,6}4 /*5 int max (int *nums,int len)6 {7 int i;8 int max=nums[0];//assumes that the No. 0 element is a maximum value9 For (i=1;i<len;i++)Ten     { One int value = *nums; A if (Value>max) -         { - max = value;//If a larger than Max is found, Max gives way the         } - nums++; -     } - return max; + } - */ +   Atypedefstruct_dog at { -     Char*name; -     intAge ; - } Dog; -typedefint(*comparefunc) (void* Data1,void*data2); - //the first address of data array to be compared, number of bytes of unitesize cell in //Size: The length of the data. {1,3,5,6}:size=4 - //Compare the data that data1 and Data2 point to, to //returns a positive number if DATA1>DATA2 + void* MAX (void* Data,intUnitSize,intsize, - comparefunc func) the { *     inti; $     Char*ptr = (Char*) data;Panax Notoginseng     Char*max = ptr;//assume that the first element is the maximum value -      for(i=1; i<size;i++) the     {       +         Char* item = ptr+i*unitsize;//calculates the first address of the first element A         if(func (Item,max) >0)//if item is greater than Max the         //How to take a few bytes to compare is inside the func thing +         { -Max =item; $         } $     } -     returnMax//the first address of the maximum value - } the   - //Data1 is the first address to be compared to a number.Wuyi intIntcompare (void* Data1,void*data2) the { -     int* Ptr1 = (int*) data1; Wu     int* PTR2 = (int*) data2; -     inti1=*ptr1; About     inti2=*ptr2; $     returnI1-I2; - } -   - intDogcompare (void* Data1,void*data2) A { +dog* Dog1 = (dog*) data1; thedog* dog2 = (dog*) data2; -     return(dog1->age)-(dog2->Age ); $     //return (Dog2->age)-(dog1->age); the } the   the intMainintargcChar*argv[]) the { -     /* in int nums[] = {3,5,8,7,6}; the int *pmax = (int *) max (nums,sizeof (int), sizeof (nums)/sizeof (int), the intcompare); About int max = *pmax; the printf ("%d\n", max); the     */ the     /*dog dogs[] ={{"sand skin", 3},{"sausage", 10},{"Husky", 5},{"Jing Ba", 8},{"Big Dog", 2};*/ +     /*Dog *pdog = (dog *) max (dogs,sizeof (dog), - sizeof (dogs)/sizeof (Dog), dogcompare); the printf ("%s=%d", pdog->name,pdog->age);*/Bayi     /*qsort (dogs,sizeof (dogs)/sizeof (dog), sizeof (dog), dogcompare); the int i; the For (i=0;i<sizeof (dogs)/sizeof (DOG); i++) -     { - Dog dog = dogs[i]; the printf ("%s=%d|", dog.name,dog.age); the     }*/ the     intNums[] = {3,5,8,7,6}; theQsort (Nums,sizeof(nums)/sizeof(int),sizeof(int), - intcompare); the     inti; the      for(i=0;i<sizeof(nums)/sizeof(int); i++) the     {94         intn =Nums[i]; theprintf"%d|", n); the     } the  98     return 0; About}

C-language function pointer: Gets the maximum of any type array

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.