分享

const用法全解

 dingzi4178 2010-04-22
const int和int const相同。
const   int   *p定义的是指向常量的整型指针.不能通过指针改变所指对象的值,但指针本身值可以改变。     int*   const   p   定义的是指针常量,指针本身的值不能改变。
const   int   *p与int const *p相同。
 
例子:
#include <stdio.h>
#include <stdlib.h>
int main()
{
 const int a = 1;
// a = 2;    //wrong
 int const b = 2;
// b = 3;    //wrong
 
 int *c = (int*)malloc(sizeof(int));
 *c = 4;    //right
 const int *d = (int*)malloc(sizeof(int));
// *d = 4;   //wrong
 d = c;  //right
 int* const e = (int*)malloc(sizeof(int));
 *e = 6;   //right
// e = c;    //wrong
 int const *f = (int*)malloc(sizeof(int));
// *f = 7;    //wrong
 f = c;     //right

 return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多