1.指针数组: 即 数组的元素是指针型;
例:int*pa[2];
明明是一维的指针数组,竟当作二维数组用。
-
- #include <iostream>
- using namespace std;
- void main()
- {
- int line1[]={1,0,0};
- int line2[]={0,1,0};
- int line3[]={0,0,1};
- int* p_line[3];
-
-
- p_line[0]=line1;
- p_line[1]=line2;
- p_line[2]=line3;
-
-
- cout<<"Matrix test:"<<endl;
- for(int i=0;i<3;i++)
- {
- for(int j=0;j<3;j++)
- {
-
- cout<<p_line[i][j]<<" ";
- }
- cout<<endl;
- }
- }
输出结果:
Matrix test:
1 0 0
0 1 0
0 0 1
2.指针型函数
当函数的返回值是地址时,该函数就叫指针形函数,又叫返回指针的函数。
声明形式:数据类型* 函数名( )
指针型函数的使用(串连接)
-
- #include <stdio.h>
- char *my_cat(char *p1, char *p2)
- {
- static char a[160],*p;
- p=a;
- while (*p1 != '\0') *p++=*p1++;
- while (*p2 != '\0') *p++=*p2++;
- *p=*p2;
- return a;
- }
-
- void main()
- {
- char s1[80],s2[80];
- printf("\n请输入第一串字符: ");
-
- gets(s1);
-
-
- fflush(stdin);
- printf("\n请输入第二串字符: ");
- gets(s2);
-
-
- printf("\n连接结果: \n");
- printf("\n第一串在前第二串在后: %s \n",my_cat(s1,s2));
- printf("\n第二串在前第一串在后: %s \n",my_cat(s2,s1));
- }
运行结果:
请输入第一串字符: dalian
请输入第二串字符: i love you
连接结果:
第一串在前第二串在后: daliani love you
第二串在前第一串在后: i love youdalian
3.指向函数的指针
3.1指向函数的指针
声明形式: 数据类型 (*函数指针名) ( 形参表);
含义: 数据指针指向的是数据存储区;而函数指针指向的是程序代码存储区。函数名就是地址。
3.2 指向函数的指针数组: 指向函数的指针可组成数组。
声明形式; 数据类型 (*函数指针名[ ] )( 形参表);

-
-
-
-
-
-
-
-
-
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef int (*pFun[])(int,int);
- int add(int,int);
- int sub(int,int);
- int mul(int ,int);
- int divide(int,int);
- void main()
- {
- pFun pfun = {mul,add,NULL,sub,NULL,divide};
-
- int item1,item2;
- int result;
- char op;
- do
- {
- printf("Please input :\n");
- fflush(stdin);
- scanf("%d %c %d",&item1,&op,&item2);
- result = pfun[op-'*'](item1,item2);
- printf("%d %c %d = %d \n",item1,op,item2,result);
- printf("try again ? please input y\\n\n");
- fflush(stdin);
- scanf("%c",&op);
- } while(op == 'y');
- }
-
- int add(int a,int b)
- {
- return (a+b);
- }
- int sub(int a,int b)
- {
- return (a-b);
- }
- int mul(int a,int b)
- {
- return a*b;
- }
- int divide(int a,int b)
- {
- if (b == 0)
- {
- exit(-1);
- }
- else
- return a/b;
- }
运行结果:

这个例子用到了+ - * / 的ASCII码
4.this指针
隐含于类中的每一个非静态成员函数中的特殊指针。
明确地指出了成员函数当前所操作的数据所属的对象。
–当通过一个对象调用成员函数时,系统先将该对象的地址赋给this指针,然后调用成员函数,成员函数对对象的数据成员进行操作时,
就隐含使用了this指针。(this是函数的第一个形参。)
5. 关于指向类成员的指针
5.1 指向类的非静态成员的指针
通过指向成员的指针只能访问公有成员
声明指向成员的指针
–声明指向公有数据成员的指针
类型说明符 类名::*指针名;
–声明指向公有函数成员的指针
类型说明符 (类名::*指针名)(参数表);
注意:应当知道,指向成员的指针名曰指针,实则非也,它是个偏移量,记录着该成员距离对象的首址的距离。
故定义它时,总要前缀着类名(类名::),以便于编译器识别后予以特别处理。

使用指向数据成员的指针
对指向数据成员的指针赋值: –说明指针应该指向哪个成员 指针名=&类名::数据成员名;
使用指向数据成员的指针: –通过对象名(或对象指针)与成员指针联手来访问数据成员
对象名.* 类成员指针名 或: 对象指针名->*类成员指针名
使用指向函数成员的指针
指向函数成员的指针
–初始化指针名=类名::函数成员名;
–通过对象名(或对象指针)与成员指针结合来访问函数成员
(对象名.* 类成员指针名)(参数表) 或: (对象指针名—>*类成员指针名)(参数表)
- #include <iostream>
- using namespace std;
- class Point
- {
- public:
- Point();
- Point(int xx,int yy);
- Point(const Point &ref);
- ~Point();
- void Move(int x,int y);
- int GetX() {return X;}
- int GetY() {return Y;}
- void Print() const
- {cout << "X=" << X <<", "<< "Y=" << Y << endl;}
- private:
- int X,Y;
- };
-
- Point::Point()
- {
- X=Y=0;
- }
- Point::Point(int xx,int yy)
- {
- X = xx;
- Y = yy;
- }
- Point::Point(const Point &ref)
- {
- X = ref.X;
- Y = ref.Y;
- }
- Point ::~Point()
- {
- }
- void Point ::Move(int x,int y)
- {
- X+=x; Y+=y;
- }
-
- void main()
- {
- Point A(4,5);
-
- Point *p1=&A;
-
-
- int(Point::*p_GetX)()=Point::GetX;
-
- cout<<(A.*p_GetX)()<<endl;
-
- cout<<(p1->GetX)()<<endl;
-
- cout<<A.GetX()<<endl;
- }
5.2 指向类的静态成员的指针
对类的静态成员(类共享)的访问不依赖于对象,可以用普通的指针来指向和访问静态成员
通过指针访问类的静态数据成员
- #include <iostream>
- using namespace std;
-
- class Point
- {
- public:
- Point(int xx=0, int yy=0) {X=xx;Y=yy;countP++;}
- Point(Point &p);
- int GetX() {return X;}
- int GetY() {return Y;}
- static int countP;
- private:
- int X,Y;
- };
-
- Point::Point(Point &p)
- {
- X=p.X; Y=p.Y; countP++;
- }
- int Point::countP=0;
-
- void main()
- {
-
- int*count = &Point::countP;
- Point A(4,5);
- cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
-
- cout<<" Object id="<<*count<<endl;
- Point B(A);
- cout<<"Point B,"<<B.GetX() <<","<<B.GetY();
-
- cout<<" Object id="<<*count<<endl;
- }
运行结果:

通过指针访问类的静态函数成员
- #include <iostream>
- using namespace std;
- class Point
- {
- public:
- Point(int xx=0, int yy=0) {X=xx;Y=yy;countP++;}
- Point(Point &p);
- int GetX() {return X;}
- int GetY() {return Y;}
- static void GetC()
- {cout<<" Object id="<<countP<<endl;}
- private:
- int X,Y;
- static int countP;
- };
-
-
- Point::Point(Point &p)
- {X=p.X; Y=p.Y; countP++; }
- int Point::countP=0;
-
- void main()
- {
-
- void (*gc)()=Point::GetC;
- Point A(4,5);
- cout<<"Point A,"<<A.GetX()<<","<<A.GetY();
- gc();
- Point B(A);
- cout<<"Point B,"<<B.GetX()<<","<<B.GetY();
- gc();
- }
运行结果:

6.指针与数组的区别
数组名是静态的,一旦定义,其值就固定不变了。而指针是动态的,可随时变化。
数组名是常量,不可作为算术运算的左值;指针是变量,可作为算术运算的左值。
在访问速度上,用数组表达式慢,用指针快。
指针比数组有更大的灵活性。
如:
char a[10] [20]; //这是个固定了行和列的矩阵
char * b[10]; //有10行,但每行可长短不等
数组有更好的可读性,可随机访问各元素;指针可读性差,更适合顺序访问。