说明:
第一篇回顾了指针的基本概念以及基本使用,因此对指针也有了一个较为清晰的思路,但实际上第一篇关于指针的内容是不太容易忘记的。这是第二篇中的内容是比较容易混淆,但对于指针的进一步学习也是非常重要的。
一.指向函数的指针
1.函数指针
·函数指针即指向函数的指针,函数指针值为函数的入口地址,通过使用该指针,即可以使用该函数;
·编写一个程序返回两个数的最大值,通过函数指针调用函数:
a.main函数代码如下:
#include<stdio.h>
int max(int *, int *);
int main(void)
{
int a, b, max_num;
int *pa, *pb;
int (*p)(int * ,int *);
printf("Please input a:");
scanf("%d", &a);pa = &a;
printf("Please input b:");
scanf("%d", &b);pb = &b;
p = max; //let p point to max funtion.
max_num = (*p)(pa, pb); //call the funtion through pointer p.
// max_num = max(pa, pb);
printf("The max number is:%d\n", max_num);
return 0;
}b.max函数代码如下:
#include<stdio.h>
int max(int *pa, int *pb)
{
if(*pa >= *pb)
return *pa;
else
return *pb;
}c.执行过程如下:
xpleaf@leaf:~/stuc/fun$ gcc -c max.c max_fun.c xpleaf@leaf:~/stuc/fun$ gcc -o max max.o max_fun.o xpleaf@leaf:~/stuc/fun$ ./max Please input a:3 Please input b:4 The max number is:4
·由上面的程序,定义函数指针方法为:
int (*p)(int *, int *) 类型名 (*指针变量名)(函数参数列表);
·使用函数指针的方法为:
p = max; ===>赋值:把函数入口地址赋给函数指针 max_num = (*p)(pa, pb); ===>调用:将原来直接的max函数调用改变为(*p)
2.用函数指针作函数参数
·使用函数指针的重要作用是把函数的地址作为参数传递到其他函数;
·编写一个程序,用函数指针作函数参数,让用户选择执行不同的功能:
a.main函数的代码如下:
#include<stdio.h>
int sum(int *, int *);
int max(int *, int *);
int min(int *, int *);
int main(void)
{
int a, b, choice, *pa, *pb;
printf("Enter two number and choose what do you want to do.\n");
printf("Please enter a:");scanf("%d", &a);pa = &a;
printf("Please enter b:");scanf("%d", &b);pb = &b;
printf("What do you want to do?\n");
printf("1.Add two number.\n2.Return the max.\n3.Return the min.\n");
scanf("%d",&choice);
if(choice == 1)
fun(sum, pa, pb);
else if(choice == 2)
fun(max, pa, pb);
else if(choice == 3)
fun(min, pa, pb);
return 0;
}b.调用其他子函数的fun函数代码如下:
#include<stdio.h>
void fun(int (*p)(int *, int *), int *pa, int *pb)
{
int result;
result = (*p)(pa, pb);
printf("The result is:%d\n", result);
}c.返回和的sum函数代码如下:
#include<stdio.h>
int sum(int *pa, int *pb)
{
return *pa+*pb;
}d.返回最大值的max函数代码如下:
#include<stdio.h>
int max(int *pa, int *pb)
{
if(*pa >= *pb)
return *pa;
else
return *pb;
}e.返回最小值的min函数代码如下:
#include<stdio.h>
int min(int *pa, int *pb)
{
if(*pa <= *pb)
return *pa;
else
return *pb;
}f.执行过程如下:
xpleaf@leaf:~/stuc/fun$ gcc -c mix.c fun.c sum.c max.c min.c xpleaf@leaf:~/stuc/fun$ gcc -o mix mix.o fun.o sum.o max.o min.o xpleaf@leaf:~/stuc/fun$ ./mix Enter two number and choose what do you want to do. Please enter a:3 Please enter b:4 What do you want to do? 1.Add two number. 2.Return the max. 3.Return the min. 1 The result is:7 xpleaf@leaf:~/stuc/fun$ ./mix Enter two number and choose what do you want to do. Please enter a:3 Please enter b:4 What do you want to do? 1.Add two number. 2.Return the max. 3.Return the min. 2 The result is:4 xpleaf@leaf:~/stuc/fun$ ./mix Enter two number and choose what do you want to do. Please enter a:3 Please enter b:4 What do you want to do? 1.Add two number. 2.Return the max. 3.Return the min. 3 The result is:3
二.返回指针值的函数
1.指针函数
·指针函数即返回值为指针的函数;
·改写函数指针中的第一个程序,使函数的返回值为一指针:
a.main函数代码如下:
#include<stdio.h>
int *max(int *, int *);
int main(void)
{
int a, b;
int *pa, *pb, *pmax;
int *(*p)(int * ,int *);
printf("Please input a:");
scanf("%d", &a);pa = &a;
printf("Please input b:");
scanf("%d", &b);pb = &b;
p = max; //let p point to max funtion.
pmax = (*p)(pa, pb); //call the funtion through pointer p.
// pmax = max(pa, pb);
printf("The max number is:%d\n", *pmax);
return 0;
}b.返回最大值函数代码如下:
#include<stdio.h>
int *max(int *pa, int *pb)
{
if(*pa >= *pb)
return pa;
else
return pb;
}c.执行过程如下:
xpleaf@leaf:~/stuc/fun$ gcc -c max.c max_fun.c xpleaf@leaf:~/stuc/fun$ gcc -o max max.o max_fun.o xpleaf@leaf:~/stuc/fun$ ./max Please input a:3 Please input b:4 The max number is:4
·由此可知,返回指针值函数的形式为:
int *max(int *, int *); 类型名 *函数名(参数列表);
·定义一个指向指针值函数的函数指针形式为:
int *(*p)(int * ,int *); 类型名 *(*指针名)(参数列表);
三.指针数组与多重指针
1.指针数组
·存放指针值的数组即为指针数组;
·编写一个程序,通过指针数组来存放书名:
a.main函数代码如下:
#include<stdio.h>
int main(void)
{
int i;
char *book[] = {
"Python",
"C",
"Linux",
"Centos",
"Ubuntu"
};
for(i = 0;i < 5;i++)
printf("%s\n", book[i]);
return 0;
}b.执行过程如下:
xpleaf@leaf:~/stuc/arry$ gcc -c arry.c xpleaf@leaf:~/stuc/arry$ gcc -o arry_book arry.o xpleaf@leaf:~/stuc/arry$ ./arry_book Python C Linux Centos Ubuntu
·为便于理解,可用如下示意图形象化:
·由此可知,定义指针数组的形式为:
char *book[10]; 类型名 *数组名[数组长度];
·注意与指向二维数组指针的区别:
int (*p)[4]; ===>指向含有4个元素的一维数组的指针
·可通过符号的优先级进行区分记忆,[ ]优先级比*高,指针数组[ ]先结合,是数组,元素值为指针;指向二维数组的指针,*先结合,是指针,指向含有4个元素的一维数组;
2.指向指针数据的指针
·因为数组的处理在编译过程中是按照指针处理的,对于上面的程序,各改为为如下:
printf("%s\n", book[i]);
改为:
printf("%s\n", *(book+i));·这与正常一维数组的处理是一致的,只是数组存放的也是指针,可能会有一混淆,可用下面图示形象理解:
·可进一步简化为:
·上面的形式为:指针-->指针(字符串首地址)-->字符串首字符,实则为多重指针;
<未完,continue>
本文出自 “香飘叶子” 博客,请务必保留此出处http://xpleaf.blog.51cto.com/9315560/1694352
原文:http://xpleaf.blog.51cto.com/9315560/1694352