Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 常量和指针(Pointers and Constants)
【标  题】:常量和指针(Pointers and Constants)
【关键字】:Pointers,and,Constants
【来  源】:http://blog.csdn.net/smilelance/archive/2007/03/06/1522383.aspx

常量和指针(Pointers and Constants)

Your Ad Here

 

常量和指针(Pointers and Constants
——const 修饰的指针解惑
 
一般遇到用const修饰的常量涉及到指针就会比较麻烦,容易把头搞晕,有个简单的技巧就是从右向左看,下面我举例子说明:
       const int* p1 = NULL; //写法一
       int const* p2 = NULL; //写法二
    int *const p3 = NULL; //写法三
以下的赋值语句,被注释掉并加上“//error”提示的是错误写法,其他是正确的:
       p1 = NULL;
       p2 = NULL;
//     *p1 = 1; //error
//     *p2 = 2; //error
//     p3 = NULL; //error
       *p3 = 0;
定义了3个指针,p1,p2,p3, 写法一和写法二是等价的,所以主要看p2和p3的区别,
 
那么根据我们从右向左的技巧,就是看const的右边:
p2:const右边是“* p2”,所定义的常量是p2所指向的常量,则p2=0正确,*p2=0错误
p2:const右边是“p3”,所定义的常量是p3这个指针本身,则p3=0错误,*p3=0正确
基于U盘传播的简单病毒:【上一篇】
directx求助,谢谢:【下一篇】
【相关文章】
  • Tomcat 6 released and stable
  • On-line Table Reorganization and Redefinition
  • SAN Extension and Routing
  • sed tricks and pitfalls
  • cnblog is cool and will post the 1st paper here
  • show log file info and log switches
  • 2.6 Qt提供的控件和对话框类(Built-in Widget and Dialog Classes)
  • 《Pointers on C》备忘
  • Understanding Complex Declarations in C/C++
  • Underdstanding Complex Declarations in C/C++
  • 【随机文章】
  • HttpHandler在VS2005发布后失效
  • 刻出好DVD盘片的十二条经验
  • 正则表达式基本语法
  • JDBC 体系结构
  • Coreldraw 初始设置要项
  • googlemap 之 javascript实现方法
  • apache的默认语言设置
  • Building NT kernel mode drivers in MS Visual Studio 8.0 (VS 2005)
  • 对称加密和非对称加密(转)
  • QQ连连看外挂 追风逐月版 代码更新
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.