函数指针
在c++中,一个函数总是占用一段连续的内从区,函数名就是该函数所占内存区的首地址。可以把函数的这个首地址(也可叫入口地址)赋予一个指针变量,使该指针指向该函数,然后通过指针变量就可以找到并调用这个函数。把这种指向函数的指针变量成为函数指针变量。
指针变量很有用。为了正确地使用函数指针,必需能够获取函数的地址、声明一个函数指针以及使用函数指针调用函数。只要单独使用函数就可以获得函数的地址。比如text(),text就是该函数的地址。要将函数作为参数进行传递,必需传递函数名。
需要声明一个函数指针时候,以下格式:
类型说明符(*指针变量名)();
例如: char(* pFunc)(int);
pFunc为一个指向函数入口的指针变量,该函数返回值是字符型。声明pFunc后,便可以将相应函数的地址赋给它,
调用一个函数指针时,有两种方法。代码:
char test(int);
char (*pFunc)(int);
pFunc=test;
char c=test(5);//普通函数调用
char t=(*pFunc)(5);//一种函数指针调用
char x=pFunc(5);//另一种函数调用
举例《如何使用函数指针。
#include<iostream>
using namespace std;
double add(double x,double y);
double minus(double x,double y);
double multiply(double x,double y);
double divide(double x,double y);
double Compute(double(*cal)(double x,double y),double x,double y);
int main()
{
int choice;
double x,y;
cout << "there are 4 types of calculations here:" << endl;
cout << "1-add,2-minus,3-multiply,4-divide"<< endl;
cout << "which one do you like to choice:";
cin >> choice;
while(choice>4||choice<1)
{
cout << "invalid choice,please choose 1-4." << endl;
cout << "which one do you like to choose." << endl;
cin >> choice;
}
cout << "input the 2 operates:" << endl;
cout << "x=" << endl;
cin >> x;
cout << "y=" << endl;
cin >> y;
switch(choice)
{
case 1:
cout << "x+y=" << Compute(add,x,y) << endl;
break;
case 2:
cout << "x-y=" << Compute(minus,x,y) << endl;
break;
case 3:
cout << "x*y=" << Compute(multiply,x,y) << endl;
break;
case 4:
cout << "x/y=" << Compute(divide,x,y) << endl;
break;
}
return 0;
}
double add(double x,double y)
{
return x+y;
}
double minus(double x,double y)
{
return x-y;
}
double multiply(double x,double y)
{
return x*y;
}
double divide(double x,double y)
{
return x/y;
}
double Compute(double(*cal)(double x,double y),double x,double y)
{
return (*cal)(x,y);
}

new 与delete
如何利用指针在程序运行时分配内存。
指针有一个强大的功能,在运行阶段分配未命名的内存以存储值。在这种情况下,只能通过指针来访问内存,即使用new,delete运算符。
例如:
int *ps=new int; //使用new分配内存
.........;//使用动态分配的内存
delete ps ; //使用完后,释放动态分配的内存
解释:上述代码在程序运行阶段要求分配一个类型为int的内存,并使用指针ps来访问这个内存,使用完毕后,用delete释放ps指向的内存。注意:delete知识释放内存,并不同时删除ps本身。一定要成对的使用new和delete。(若只是用new,不用delete释放,则会发生内存泄露,即被分配的内存再也无法使用。)不要用delete释放声明变量获得的内存,也不要释放已经释放的内存。
new和delete还可以创建动态数组,因为数组的特殊性,所以创建动态数组的方式也有所不同,如下:
int * pArray=new int[8]; //创建动态数组
delete [ ] pArray; // 释放
例子:
#include<iostream>
using namespace std;
int main()
{
int i;
int *pc=new int;
double *pd=new double;
char *pArray=new char[8];
*pc=100;
*pd=6.28;
for(i=0;i<8;i++)
{
pArray[i]='A'+i;
}
cout << "*pc=" << *pc << ",size of pc =" << sizeof pc << ",size of *pc=" << sizeof * pc << endl;
cout << "*pd=" << *pd << ",size of pd= " << sizeof pd << ",size of *pd=" << sizeof * pd << endl;
cout << "here are the elements of the dynamic array:" << endl;
for(i=0;i<8;i++)
{
cout << pArray[i];
}
cout << endl;
delete pc;
delete pd;
delete [] pArray;
return 0;
}
结果:

动态存储的应用:
要求:一个存储学生信息的数组,对于程序来说,学生的数量是不确定的,程序通过动态内存分配接受用户的输入,整理之后在用表的形式打印出来。
代码:
#include<iostream>
using namespace std;
struct stuInfo{
char name[30];
int age;
int grade;
int stuNo;
}stuInfo;
int main()
{
int num,i;
struct stuInfo *tmp;
cout << "how many students ?" << endl;
cin >> num;
tmp=new struct stuInfo[num];
cout << "now you can input data:" << endl;
i=0;
while(num)
{
cout << "NO." << i+1 << endl;
cout << "name:";
cin >> tmp[i].name;
cout << "age:" ;
cin >>tmp[i].age;
cout << "grade:";
cin >> tmp[i].grade;
cout << "stuNo:";
cin >> tmp[i].stuNo;
num--;
i++;
}
cout << "here are the results:" << endl;
cout << "name\tage\tgrade\tstudentNO" << endl;
while(i)
{
cout << tmp[num].name << "\t";
cout << tmp[num].age << "\t";
cout << tmp[num].grade << "\t";
cout << tmp[num].stuNo << endl;
i--;
num++;
}
return 0;
}
运行结果:

指针到此为止。下面进入类和对象的学习。