指针常量与常量指针,函数指针与指针函数,指针的指针
2011-03-28 14:59
190 查看
一、指针常量与常量指针
(1)指针常量:指针本身是常量,它不能被改变(也就是说:一旦让它指向某一个变量或对象,就不能再让它指向别的变量或对象)。但它所指向的内容可以改变。
格式:
指针类型 *const 指针常量名=&变量名;
eg : int a; int *const ip2=&a;
(2)常量指针:指针所指向的内容是常量,不能改变,但指针本身可以改变。
格式:
数据类型 const * 常量指针名称;
或
const 数据类型 * 常量指针名称;
eg: int a; const int *ip=&a;
(3)example:
#include<iostream>
using namespace std;
int main()
{
int a=100;
int b=200;
/*------------------------常量指针-----------------------------------*/
const int *ip=&a; //定义常量指针
cout<<"*ip="<<*ip<<endl;
//*ip=200; //错误,指针所指向的内容是常量,不能改变
ip=&b; //正确,指针本身可以改变
cout<<"*ip="<<*ip<<endl;
/*------------------------指针常量-----------------------------------*/
int *const ip2=&a; //定义指针常量
cout<<"*ip2="<<*ip2<<endl;
//ip2=&b; //指针本身是常量,它不能被改变
*ip2=200; //正确,它所指向的值可以改变
cout<<"a="<<a<<endl;
cout<<"*ip2="<<*ip2<<endl;
return 0;
}
二、函数指针与指针函数
(1)指针函数:返回值为一个指针的函数,实际上就是返回一个地址给调用函数。
格式:
类型说明符 * 函数名(参数)
eg: int *fun(int a,int b);//由于返回的是一个地址,所以类型说明符一般都是int。
函数返回的是一个地址值,经常使用在返回数组的某一元素地址上。
(2)函数指针:一个指向函数的指针。可以通过它来调用函数。
格式:
类型说明符 (*函数名)(参数)
eg: int (*fp)(int a,int b);
例如:
void (*fp)();
把函数的地址赋值给函数指针,可以采用下面两种形式:
fp=&function;
fp=function;
取地址运算符&不是必需的,因为单单一个函数标识符就标号表示了它的地址,如果是函数调用,还必须包含一个圆括号括起来的参数表。
可以采用如下两种方式来通过指针调用函数:
x=(*fp)();
x=fp();
两种作用是一样的。
(3)example
#include<iostream>
using namespace std;
char *Get();
int main()
{
cout<<Get()<<endl;
return 0;
}
char *Get()
{
return "Test successful!";
}
//***********************************************/
//用函数指针实现函数的调用
#include<iostream>
using namespace std;
void (*fp)();
void Funa();
int main()
{
fp=Funa;//等价于fp=&Funa
(*fp)();
cout<<"***********************"<<endl;
fp();
return 0;
}
void Funa()
{
cout<<"This is Funa!"<<endl;
}
三、指针的指针
(1)eg: char **pc;
如果有三个星号,那就是指针的指针的指针.
(2)通过指针的指针,不仅可以访问它指向的指针,还可以访问它指向的指针所指向的数据。
(3)example:
//查找一个数组中<=0的第一个数及其所在位置
#include<iostream>
using namespace std;
void Search(int **);
int count=0;
int main()
{
int a[]={10,8,-2,888,0,6,9};
int *fpp=a;
Search(&fpp);
if(count<=sizeof(a)/sizeof(int))
cout<<*fpp<<":"<<count<<endl;
else
cout<<"nou found!"<<endl;
return 0;
}
void Search(int **fpps)
{
while(**fpps!=0)
{
count++;
if(**fpps<0)
break;
else
(*fpps)++;//*与++优先级相同,但结合性自右至左,所以要加括号
//x=*p++;等价于x=*(p++);
}
}
(1)指针常量:指针本身是常量,它不能被改变(也就是说:一旦让它指向某一个变量或对象,就不能再让它指向别的变量或对象)。但它所指向的内容可以改变。
格式:
指针类型 *const 指针常量名=&变量名;
eg : int a; int *const ip2=&a;
(2)常量指针:指针所指向的内容是常量,不能改变,但指针本身可以改变。
格式:
数据类型 const * 常量指针名称;
或
const 数据类型 * 常量指针名称;
eg: int a; const int *ip=&a;
(3)example:
#include<iostream>
using namespace std;
int main()
{
int a=100;
int b=200;
/*------------------------常量指针-----------------------------------*/
const int *ip=&a; //定义常量指针
cout<<"*ip="<<*ip<<endl;
//*ip=200; //错误,指针所指向的内容是常量,不能改变
ip=&b; //正确,指针本身可以改变
cout<<"*ip="<<*ip<<endl;
/*------------------------指针常量-----------------------------------*/
int *const ip2=&a; //定义指针常量
cout<<"*ip2="<<*ip2<<endl;
//ip2=&b; //指针本身是常量,它不能被改变
*ip2=200; //正确,它所指向的值可以改变
cout<<"a="<<a<<endl;
cout<<"*ip2="<<*ip2<<endl;
return 0;
}
二、函数指针与指针函数
(1)指针函数:返回值为一个指针的函数,实际上就是返回一个地址给调用函数。
格式:
类型说明符 * 函数名(参数)
eg: int *fun(int a,int b);//由于返回的是一个地址,所以类型说明符一般都是int。
函数返回的是一个地址值,经常使用在返回数组的某一元素地址上。
(2)函数指针:一个指向函数的指针。可以通过它来调用函数。
格式:
类型说明符 (*函数名)(参数)
eg: int (*fp)(int a,int b);
例如:
void (*fp)();
把函数的地址赋值给函数指针,可以采用下面两种形式:
fp=&function;
fp=function;
取地址运算符&不是必需的,因为单单一个函数标识符就标号表示了它的地址,如果是函数调用,还必须包含一个圆括号括起来的参数表。
可以采用如下两种方式来通过指针调用函数:
x=(*fp)();
x=fp();
两种作用是一样的。
(3)example
#include<iostream>
using namespace std;
char *Get();
int main()
{
cout<<Get()<<endl;
return 0;
}
char *Get()
{
return "Test successful!";
}
//***********************************************/
//用函数指针实现函数的调用
#include<iostream>
using namespace std;
void (*fp)();
void Funa();
int main()
{
fp=Funa;//等价于fp=&Funa
(*fp)();
cout<<"***********************"<<endl;
fp();
return 0;
}
void Funa()
{
cout<<"This is Funa!"<<endl;
}
三、指针的指针
(1)eg: char **pc;
如果有三个星号,那就是指针的指针的指针.
(2)通过指针的指针,不仅可以访问它指向的指针,还可以访问它指向的指针所指向的数据。
(3)example:
//查找一个数组中<=0的第一个数及其所在位置
#include<iostream>
using namespace std;
void Search(int **);
int count=0;
int main()
{
int a[]={10,8,-2,888,0,6,9};
int *fpp=a;
Search(&fpp);
if(count<=sizeof(a)/sizeof(int))
cout<<*fpp<<":"<<count<<endl;
else
cout<<"nou found!"<<endl;
return 0;
}
void Search(int **fpps)
{
while(**fpps!=0)
{
count++;
if(**fpps<0)
break;
else
(*fpps)++;//*与++优先级相同,但结合性自右至左,所以要加括号
//x=*p++;等价于x=*(p++);
}
}
相关文章推荐
- (指针常量,常量指针)-(函数指针,指针函数)-(指针数组,数组指针)
- 指针函数和函数指针,指针常量和常量指针
- 函数指针、指针函数、常量指针、指针常量、指向常量的常量指针
- 常量指针、指针常量、指针函数、函数指针、指针数组、数组指针区别在哪里?
- 指针常量、常量指针;指针数组、数组指针;指针函数、函数指针
- 指针的几种形式(常量指针和指针常量,指针数组和数组指针,指针函数和函数指针,数组和指针)
- 指针常量和常量指针、函数指针和指针函数、指针数组和数组指针
- C++ 指针,常量,数组,函数,模板,内联等混淆概念
- 指针函数和函数指针
- 指针函数,函数指针
- 指针函数与函数指针的区别
- 【转】 C++易混知识点2. 函数指针和指针函数的区别
- 关于指针数组、数组指针、指针函数、函数指针的归纳小结
- 指针函数与函数指针
- C语言指针导学(4)——分清函数指针和指针函数
- 指针函数与函数指针的区别
- 指针函数与函数指针
- 黑马程序员———C语言———【函数指针、指针函数、野指针、内存管理、const关键字】
- C/C++沉思-----指针函数和函数指针
- 指针数组、数组指针、指针函数、函数指针的简单介绍