---
title: "c++指针续"
author: "yunchow"
date: "2009-05-23T11:16:14Z"
tags: ["C", "C#", "C++", "FP"]
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2009-05-23T11:16:14Z"
id: 394002
url: "https://yunchow.iteye.com/blog/394002"
---

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