#include <stdio.h>
typedef void (*intFunc)(int i); void test1(int age)
{
printf("test1:%d\n",age);
}
int test2(char *str,int i)
{
}
void foreachNums(int *nums,int len,intFunc func)
{
int i;
for(i=;i<len;i++)
{
int n = nums[i];//*(nums+i)
func(n);
}
}
void printIt(int i)
{
printf("value=%d\n",i);
}
void print100(int i)
{
printf("value=%d\n",i+);
}
int main(int argc, char *argv[])
{
//intFunc f1 = test1;//intFunc类型的函数指针指向test1函数
//f1(8);//执行f1函数指针指向的代码
//intFunc f2 = test2;
int nums[]={,,,,};
//foreachNums(nums,sizeof(nums)/sizeof(int),printIt);
foreachNums(nums,
sizeof(nums)/sizeof(int),print100);
return ;
}
http://www.rupeng.com/Segments/Index/1752
-----------------------------------------------------
#include <stdio.h> //{3,5,8,7,6}
/*
int max(int *nums,int len)
{
int i;
int max=nums[0];//假定第0个元素为最大值
for(i=1;i<len;i++)
{
int value = *nums;
if(value>max)
{
max = value;//如果找到比max还大的,则max让位
}
nums++;
}
return max;
}
*/ typedef struct _Dog
{
char* name;
int age;
} Dog;
typedef int (*compareFunc)(void * data1,void * data2);
//data 待比较数据数组的首地址,uniteSize单元字节个数
//size:数据的长度。{1,3,5,6}:size=4
//比较data1和data2指向的数据做比较,
//如果data1>data2,则返回正数
void* max(void* data,int unitSize,int size,
compareFunc func)
{
int i;
char *ptr = (char *)data;
char *max = ptr;//假定最开始的元素为最大值
for(i=;i<size;i++)
{
char* item = ptr+i*unitSize;//计算第i个元素的首地址
if(func(item,max)>)//如果item大于max
//到底取几个字节进行比较是func内部的事情
{
max = item;
}
}
return max;//最大值的首地址
} //data1是第一个被比较数字的首地址
int intCompare(void* data1,void *data2)
{
int* ptr1 = (int*)data1;
int* ptr2 = (int*)data2;
int i1=*ptr1;
int i2=*ptr2;
return i1-i2;
} int dogCompare(void* data1,void* data2)
{
Dog* dog1 = (Dog*)data1;
Dog* dog2 = (Dog*)data2;
return (dog1->age)-(dog2->age);
//return (dog2->age)-(dog1->age);
} int main(int argc, char *argv[])
{
/*
int nums[] = {3,5,8,7,6};
int *pMax = (int *)max(nums,sizeof(int),sizeof(nums)/sizeof(int),
intCompare);
int max = *pMax;
printf("%d\n",max);
*/
/*Dog dogs[] ={{"沙皮",3},{"腊肠",10},{"哈士奇",5},{"京巴",8},{"大狗",2}};*/
/*Dog *pDog = (Dog *)max(dogs,sizeof(Dog),
sizeof(dogs)/sizeof(Dog),dogCompare);
printf("%s=%d",pDog->name,pDog->age);*/
/*qsort(dogs,sizeof(dogs)/sizeof(Dog),sizeof(Dog),dogCompare);
int i;
for(i=0;i<sizeof(dogs)/sizeof(Dog);i++)
{
Dog dog = dogs[i];
printf("%s=%d|",dog.name,dog.age);
}*/
int nums[] = {,,,,};
qsort(nums,sizeof(nums)/sizeof(int),sizeof(int),
intCompare);
int i;
for(i=;i<sizeof(nums)/sizeof(int);i++)
{
int n = nums[i];
printf("%d|",n);
} return ;
}
http://www.rupeng.com/Segments/Index/1754