There are two type of const:
1. const int *p; <----- pointer 'p' is not const, while to what it points is const.
e.g.:
int i;
const int ci = 5;
const int *p;
p = &i;
p = &ci;
*p = 10; // error here
2. int *const cp; <----- the pointer 'cp' is const, you can't reassign value to 'cp' twice.
e.g.:
int i;
int j;
int *const cp = &i;
*cp = 3;
*cp = 4;
cp = &j; // error here
together:
const.c
1 #include<stdio.h>
2 #include<stdlib.h>
3 void main()
4 {
5 int i = 5;
6 int j = 7;
7 const int ci = 4;
8 const int *p;
9 int *const cp = &j;
10 const int *const cp1 = &ci; // both pointer and what it points to are const.
11 p = &ci;
12 p = &i;
13 printf("*p=%d\n", *p);
14 printf("*cp=%d\n", *cp);
15 printf("*cp1=%d\n", *cp1);
16 const int **pp;
17 pp = &p;
18 printf("**pp=%d\n", **pp);
19 const int *const *const cpp = &cp1;
20 printf("**cpp=%d\n", **cpp);
21 exit(EXIT_SUCCESS);
22 }
分享到:
相关推荐
const char* pCh; // 指向字符串常量的指针; 表示pch是一个指针,指向字符串常量。根据需要还可以指向另一个字符串。 但字符串不能被改变。 char* const pCh; // 指向字符串的常量指针; 表示pch是一个字符串常...
Const,Const函数,Const变量,函数后面的Const Const,Const函数,Const变量,函数后面的Const
C++:关于const的思考.doc onst的思考 1、什么是const? 常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。(当然,我们可以偷梁换柱进行更新:) 2、为什么引入const? const...
关于C语言中const的用法。以下摘自原文:In my last column, I discussed one of the reasons why the rules by which a compiler can place data into ROM are a bit more complicated in C++ than they are in C. ...
本文章是关于C语言中const定义常量 。
static,const,static const,const static是c++中几个常见的很容易弄混淆的几个概念,在这里总结了一下,与大家分享!
为了程序的健壮型,减少程序出错,这里详细介绍了const的用法。
Dan_Saks 关于C语言中有关const的含义和用法剖析的英文原版文章
易语言 常量支持库.1.6.版本const.fne
本文给大家介绍了C++中的const限定修饰符。
关于const的详细讲解,非常有用!!!!!!
C++中的各种Const用法小结:const常量,const 修饰类的数据成员等等
const int *a int * const a 区别 外加实例
const在C++中占有重要作用,属于小兵立大功的典型,本文档详细介绍了如何使用const
c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法
现在读到了const关键字的用法,书上面讲解的时候并没有给出完整的实例,只是理论的讲解了一些知识,后来在网上面搜了关于const的关键字的讲解,但是搜到的内容和书上面讲解的内容有着非常让人疑惑的东西,看看下面的...
一 const基础;二 const的初始化;三 作为参数和返回值的const修饰符;四 类成员函数中const的使用;五 使用const的一些建议。
该文档讲述了const与指针的关系,const起了如何的作用。
const和volatile分析 这个分析得很好 面试 找工作 必备的
C++中const关键字详解