文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>简单说明用const修饰指针

简单说明用const修饰指针

时间:2010-04-18  来源:pengyitsun

通过实验简单地说明如何使用const来修饰指针,包括const声明时的位置

#include <stdio.h>
void func(const int * p);


int main()
{
    int c=9;
    int d=10;
    const int * b=&c;

    c=11;

   

    //*b=12;

    //如果该行存在,则不会通过编译。即无法通过b来修改c的值。
    printf("Value of b is:%d\n",*b);
    b=&d;//如果这样声明变量b:“int * const b=&c”,那么不会通过编译。

          //即b不能再指向其他变量。
    printf("After change the reference of b, the value of *b is:%d\n",*b);

   
    printf("Before calling the function: c1=%d\n",c);
    func(&c);
    printf("After calling the function: c2=%d\n",c);
    return 0;
}


void func( const int * p)
{
    printf("Before change the p point: p1=%d\n",*p);
    int d=1092;
    p=&d;//It is legal to operate on p.
    printf("After change the p point: p2=%d\n",*p);
}


总结:

const type * variable;//const修饰所指的值

type * const variable;//const修饰指针

const type * const variable;//const修饰指针、修饰所指的值(组合形式)

(END)

相关阅读 更多 +
排行榜 更多 +
开心动动脑安卓版 v1.0 手机版

开心动动脑安卓版 v1.0 手机版

休闲益智 下载
不良人破局手游下载

不良人破局手游下载

角色扮演 下载
云海之下手游下载

云海之下手游下载

角色扮演 下载