您的位置:首页 > 编程语言 > C语言/C++

C++基础复习---1(函数指针,指针函数)

2015-09-24 21:58 363 查看
(1)定义

    函数名即为指向函数第一条指令的指针,本质是一个指针变量。编译器为每个函数分配一个首地址,即为函数第一条指令的地址。

一般情况下用一个指针保存这个地址,即为函数指针。也作为指向函数的别名。

定义方式:

类型名 (*指针变量名)(参数1,参数2.....)

type (*fp)(type&, type &);

(2)例子-1  打印输出最小值

#include <stdio.h>

void PrintMin(int a, int b){
if (a < b)
{
printf("%d\n", a);
}
else{
printf("%d\n", b);
}

}


int main()

{
void(*pf)(int, int);
int x = 4, y = 5;
pf = PrintMin;
pf(x, y);


        return 0;


}

以上例子仅仅是一个简单应用,可能还是不太懂到底有啥用,请看例子2

(3)例子-2  qsort函数的使用



qsort   功 能: 使用快速排序例程进行排序   

用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));   

各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针
其中这个比较函数,就是一个指向函数的指针,可以自行定义传入。例如现在要将一个数组按照个位数升序
比较函数都是两个元素作为参数,返回一个int值:

1)返回>0,则qsort认为a>b

2)返回=0,则qsort认为a=b

3)返回<0,则qsort认为a<b,即采用从小到大排序

可以定义如下:

1)对int类型数组排序,按照个位数升序

int MyCompare(const void * elem1, const void *elemt2){
unsigned int *p1, *p2;
p1 = (unsigned int *)elem1;
p2 = (unsigned int *)elemt2;
return (*p1 % 10) - (*p2 % 10);//

}


2)对char类型数组排序

int MyCompare(const void * elem1, const void *elemt2){

    return *(char*)a -*(char*)b;

}

3)对double类型数组排序

int MyCompare(const void * elem1, const void *elemt2){


  return *(double*)a > *(double*)b ? 1: -1;

}

4)对结构体一级排序

struct HH{double data; int other;}

int MyCompare(const void * elem1, const void *elemt2){


   return (*HH*)a).data > (*(HH*).data? 1: -1;

}

5)对结构体二级排序

int MyCompare(const void * elem1, const void *elemt2){


   struct HH* c = (HH*)a;

   struct HH* d = (In*)b;

   if(c->other != d->other)

      return c->other - d->other;

else

    return d->other - c->other;

}



6) 对字符串排序

struct In { int data; char str[100]; }s[100];


int MyCompare(const void * elem1, const void *elemt2){


     return strcmp( (*(In *)a)->str , (*(In *)b)->str );

}

从而最终的调用可写为:

#include <stdio.h>

#include <stdlib.h>


int main()

{
unsigned int an[5] = { 8, 13, 11, 10, 34 };
qsort(an, 5, sizeof(unsigned int), MyCompare);

for (int i = 0; i < 5; ++i)
{
printf("%d ", an[i]);
}
return 0;

}


(4)指针函数

   指针函数是指带指针的函数,本质是一个函数,函数返回某一类型的指针

   类型标识符  *函数名(参数表)

   int *f(x, y)

    首先这是一个函数,只不过返回值是一个地址。注意与函数指针的区别,最简单的辨别方式就是看函数名前面的指针*有没有被括号,如果被()包含就是函数指针,否则就是指针函数

使用:



//指针函数

int *GetDate(int wk, int dy)

{

static int calendar[5][7] =

{

{ 1, 2, 3, 4, 5, 6, 7 },

{ 8, 9, 10, 11, 12, 13, 14 },

{ 15, 16, 17, 18, 19, 20, 21 },

{ 22, 23, 24, 25, 26, 27, 28 },

{ 29, 30, 31, -1 }

};

return &calendar[wk - 1][dy - 1];

}

int main()

{

//函数指针

printf("\n测试函数指针\n");

void(*pf)(int, int);

int x = 4, y = 5;

pf = PrintMin;

pf(x, y);

//

unsigned int an[5] = { 8, 123, 11, 10, 4 };

qsort(an, 5, sizeof(unsigned int), MyCompare);

for (int i = 0; i < 5; ++i)

{

printf("%d ", an[i]);

}

//指针函数

printf("\n测试指针函数\n");

int wk, dy;

do{

printf("Enter week(1 - 5)day(1 - 7)\n");

scanf("%d%d", &wk, &dy);

} while (wk<1 || wk>5 || dy<1 || dy>7);

printf("%d\n", *GetDate(wk, dy));

return 0;

}


总结:

主要的区别是一个是指针变量,一个是函数。在使用是必要要搞清楚才能正确使用




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: