const的用法

来源:互联网 发布:阿里数据研究院 编辑:程序博客网 时间:2024/06/07 00:14

用一个实例代码来记录一下使用const定义的变量可以怎样赋值

#include <stdio.h>int main(){    int b = 10;    const int a = b;    int const c = b;    a = 10; //error, con't modify read-only.    c = 11; //error, con't modify read-only.    const int *e = &b;  //*e is const, but e is not read-only.    int f = 20;    *e=13; //error, con't modify read-only.    e= &f; //ok.    int * const g = &b; //g is const, but *g is not read-only.    *g = 21;//ok    g = &f; //error.    return 0;}
0 0
原创粉丝点击