![]() // zhizhen.cpp : 定义控制台应用程序的入口点。
// zhizhen.cpp : 定义控制台应用程序的入口点。
![]()
![]() /**//*
/**//*
![]() 函数指针的例子
    函数指针的例子
![]() 061105
    061105
![]() */
*/
![]()
![]()
![]() #include <stdio.h>
#include <stdio.h>
![]() #include <stdlib.h>
#include <stdlib.h>
![]()
![]()
![]() typedef int (*FUN)(int a ,int b);        //函数指针类型,功能和下面main方法中的函数指针一样
typedef int (*FUN)(int a ,int b);        //函数指针类型,功能和下面main方法中的函数指针一样
![]() int compare(const void* a, const void* b);        //声明compare
int compare(const void* a, const void* b);        //声明compare
![]()
![]() char* list[5]=
char* list[5]=![]() {"cattle","car","cabet","cap","canon"};        //全局字符串数组
{"cattle","car","cabet","cap","canon"};        //全局字符串数组
![]() static int i = 0;
static int i = 0;
![]()
![]() /**//*
/**//*
![]() 函数指针的例子
    函数指针的例子
![]() */
*/
![]() int add(int a,int b)
int add(int a,int b)
![]()
![]()
![]() {
{
![]() return a+b;
    return a+b;
![]() }
}
![]()
![]()
![]() /**//*
/**//*
![]() 测试函数指针的例子
    测试函数指针的例子
![]() */
*/
![]() void test()
void test()
![]()
![]()
![]() {
{
![]() //int (*fPtr)(int a,int b);    //定义一个函数指针
    //int (*fPtr)(int a,int b);    //定义一个函数指针
![]() FUN fPtr;
    FUN fPtr;
![]() fPtr = add;
    fPtr = add;
![]() printf("8 add 4 is:%d",fPtr(8,4));
    printf("8 add 4 is:%d",fPtr(8,4));
![]() scanf(" ");
    scanf(" ");
![]() }//test
}//test
![]()
![]()
![]()
![]() /**//*
/**//*
![]() 函数指针作函数参数
    函数指针作函数参数
![]() */
*/
![]() void SortWithCompare()
void SortWithCompare()
![]()
![]()
![]() {
{
![]() int i = 0;
   int i = 0;
![]() //为了达到一种通用性.这里使用qsort时要把数组转成void*型的,qosrt的函数声明如下(详细qsort见qsort.c)
   //为了达到一种通用性.这里使用qsort时要把数组转成void*型的,qosrt的函数声明如下(详细qsort见qsort.c)
![]()
![]() /**//*
    /**//*
![]() void __cdecl qsort (
        void __cdecl qsort (
![]() void *base,
                    void *base,
![]() size_t num,
                    size_t num,
![]() size_t width,
                    size_t width,
![]() int (__cdecl *comp)(const void *, const void *)
                    int (__cdecl *comp)(const void *, const void *)
![]() )
                    )
![]() */
    */
![]() //数组类型有多少个,比较大小的方法就有多少了,这样的话,编译器是写不过来的,这里有没有面向对像的装箱和拆箱
   //数组类型有多少个,比较大小的方法就有多少了,这样的话,编译器是写不过来的,这里有没有面向对像的装箱和拆箱
![]() //虽然这里char **与void*的转换有点像装箱和拆箱.
   //虽然这里char **与void*的转换有点像装箱和拆箱.
![]() //所以,比较两个元素的方法就要自己写了,试想一下还有结构体数组也要来排序怎么办??
   //所以,比较两个元素的方法就要自己写了,试想一下还有结构体数组也要来排序怎么办??
![]() qsort((void*)list,5,sizeof(list[0]),compare);
  qsort((void*)list,5,sizeof(list[0]),compare);        
![]() for(i=0; i<5; i++)
  for(i=0; i<5; i++)
![]() printf("%s\n",list[i]);
    printf("%s\n",list[i]);
![]()
![]() }//SortWithCompare
}//SortWithCompare
![]()
![]()
![]() /**//*
/**//*
![]() 用于向qsort中传入用来比较字符串数组中的任两个元素的大小
    用于向qsort中传入用来比较字符串数组中的任两个元素的大小
![]() */
*/
![]() int compare(const void* a, const void* b)
int compare(const void* a, const void* b)
![]()
![]()
![]() {
{
![]() i++;
    i++;
![]() printf("第%d次使用compare:a=%s,b=%s\n",i,*(char**)a,*(char**)b);
    printf("第%d次使用compare:a=%s,b=%s\n",i,*(char**)a,*(char**)b);
![]() return strcmp(*(char**)a, *(char**)b);
    return strcmp(*(char**)a, *(char**)b);
![]() }//compare
}//compare
![]()
![]() void main()
void main()
![]()
![]()
![]() {
{
![]() SortWithCompare();
    SortWithCompare();
![]() scanf(" ");
    scanf(" ");
![]() }//main
}//main
上面是一个小例子,觉得在C中的指针和C#的代理是一个意思.大家可以对比去看看