//常指针,或叫定指针:指针指向一个规定的地址,不能指向别处//常指针表示这个指针是常量.就像指南针,不能修改.int * const p;//常指针,禁止修改*Pint n;p = &n; //这样写可以,但是不能通过*p来修改nconst int * q; //指向常量的指针const int * const r; //指向常量的常指针.
指针都占4字节内存空间,因为它只要保存地址
//指向不确定类型指针
void * p;//可用来保存各种类型的地址
#include <iostream.h>using namespace std;int main(){ void * p = NULL; int n = 100; double d = 20.34; p = &n; cout << *(int *)p << endl; //这里需要强转,不然是会报错的 p = &d; cout << *(double *)p << endl;}char buf [ ] = "good bye";char * p = "good bye";sizeof(buf);//9sizeof(p);//4 因为P是一个指针,就是一个地址,因此是4int main(int argc, char * argv[ ] )//系统会把命令行中字符串的字符个数传过来//argc 命令行字符串的个数//argv[] 依次指向命令行中的字符串#include <iostream.h>using namespace std;int main ( int argc, char* argv[] ){ cout << "You input the command : " << argv[0] << endl; cout << "Args has : " << argc << endl; cout << "They are : "; for( int i=1; i<argc; i++ ) cout << argv[i] << ' '; cout << endl;}#include <iostream>#include <cstdlib>using namespace std;int main(int argc, char* argv[]){ if(argc-1==0) { cout << "Thank you!" << endl; return 0; } double n=0.0; for( int i=0; i< argc; i++ ) n += atof ( argv[i] ); //注意atof(),atoi()函数的用法,可用man查询 cout << "The average is : " << n/(argc-1) << endl;}
---------------------------------------
* 引用
一个变量的类型和地址是终身不变的.
同理变量别名也是终身不变的.
引用就是变量的别名.
#include <iostream>using namespace std;int main(){ int n = 100; int m = n; int * p = &n; int & rn = n; cout << "&n = " << &n << endl; cout << "&m = " << &m << endl; cout << "&p = " << &p << endl; cout << "&rn = " << &rn << endl;//&rn == &*p;}
引用是聪明的指针.
起名规则:引用r开头,指针变量p开头.
c++中提倡用引用而不是指针,只要能用引用解决不用指针.
引用直接操作初始它的那个变量,类型还是要一致.
引用更方便也更安全.
常量引用:1,接受常量初绐化.2,禁止修改这个引用.(const int& r)
**尽量用引用来传递参数,尽量用const来禁止对引用的修改.
与一般的参数传递相比:常量引用不会复制一份数据,提高效率了.有的变量禁止复制,
可解决此类问题.
函数的返回类型同样可写成引用,不会复制数据.
=====================================================
* 函数指针
* 使用指针和引用要注意的事项
指针是用来保存地址的,引用是变量的别名,它们都是用来访问原始变量的.
要保证那个变量有"以后",也就是说,原始变量的生命周期要够长,不然指针也
变成野指针了,说白了就是函数返回的时候,此原始变量还存在.指针在使用的时候
一定要初始化(NULL).同理,函数的形参是在调用函数的时候由实参来初始化的.
不初始化是垃圾数据,除全局变量,静态变量外.默认初始化为0.还是建议自已进行
合理的初始化,一般初始化成0.
指针与引用,首选引用,因为它更安全,方便.
用指针保证指向有效,NULL指针没有指向任何地方.
当delete后,指针依然指向已释放的空间,所以将其置NULL,这一点很重要.
函数名就表示函数的地址,不用再对其进行取地址了.
函数指针定义:像声明函数一样,定义函数指针,不同在于,要把函数名改成(*指针名)的形式.
函数指针在指向的时候也需要类型一致.
//示例程序#include <iostream>using namespace std;void reset( int a[ ] , int n );void input( int a[ ] , int n );void output( int a[ ] , int n );void sort( int a[ ] , int n );int main(){ void (*fp) ( int a[], int n )=NULL;//函数指针的定义与声明一样 int x[5]; fp = output;//给函数指针赋值,因为函数名就是函数地址 output( x, 5 ); fp( x, 5 );}void output( int a[ ] , int n ){ /* output code ... */}
//函数指针示例程序.#include <iostream>#include <cstring>using namespace std;void reset( int a[ ] , int n );void input( int a[ ] , int n );void output( int a[ ] , int n );void sort( int a[ ] , int n );int main(){ void (*fp) ( int a[], int n )=NULL;//函数指针的定义与声明一样 int x[5]; fp = output;//给函数指针赋值,因为函数名就是函数地址 fp( x , 5 ); output( x, 5 ); fp = reset; fp(x , 5 ); fp = output; fp( x, 5 );}void output( int a[ ] , int n ){ for( int i=0; i<n; i++ ) cout << a[i] << ' '; cout << endl;}void reset( int a[ ] , int n ){ memset( a, 0, sizeof(int)*n);//用特定的值初始化一块内存,可用man查看memset() //for(int i = 0; i< n ; i++ ) // a[i] = 0;}
相关资源:征服指针(C)