首页
社区
课程
招聘
[原创]看c和指针关于const的理解,大牛勿喷,只是个人理解
发表于: 2015-9-26 14:56 5098

[原创]看c和指针关于const的理解,大牛勿喷,只是个人理解

2015-9-26 14:56
5098

#include <stdio.h>

void main()
{
        //int const i ; //const object must be initialized if not extern。变量const i如果不是一个外部变量(extern),在声明时必须初始化。(1)
        int const i = 5;
        int *pi;
        int const *cpi;             //可以不初始化
        int * const pci = NULL;     //必须初始化
        int a, b /* c */;

        pi = &a;
        cpi = &b;
        //*cpi = 10;               //error : *cpi's value is const, cannot be modified。 cpi是个指针变量,而*cpi是个常量被修饰为const不可变,故给*cpi赋值会报错 (2)
        cpi = &a;                  //correct: cpi is a value can be modified 。cpi是个变量值可以被修改。
        cpi = &b;

        *pci = 10;                //correct
   // pci = &a;               //error:  pci can not be modified。pci是一个const变量,不可变,而他指向的值可变。

    const int *p1;            //.c文件这样写编译不通过,.cpp可以.在VC6.0下const int *p1和int const *p2编译不通过,CODEBLOCKS就可以编译
    int const *p2;

        //*p1 = 10 ;              //error
        //*p2 = 15;               //error
    p1 = &a;                  //correct
        p2 = &b;                  //const int *p与int const *p 这种写法没区别

    int const * const cpci;   //在VC6.0下源文件为.c编译不通过。CODEBLOCKS就可以编译
    //最后关于const修饰的指针变量可以这么理解
    //把const 看成是一个类型而int也是个类型,*p也看成是个类型,类型和类型结合,变量名看成是个个体
    //const int *p (counst int) (*p)而const int是不可变的所以*p不可变,但是p可变
    //int const *p (int count) (*p)同上
    //int * const pci(int * const)(pci) pci不可变,*pci可变,且必须初始化。

}

收藏
免费 3
支持
分享
最新回复 (4)
雪    币: 204
活跃值: (911)
能力值: (RANK:1324 )
在线值:
发帖
回帖
粉丝
2
int * const pci = NULL;
*pci = 10;
这样不会报错么?
2015-9-26 15:31
0
雪    币: 44
活跃值: (385)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
不会报错的,*pci是可变的
2015-9-26 15:38
0
雪    币: 44
活跃值: (385)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
这个代码可以在CODEBLOCKS下编译通过,VC6.0下也可以的注释写了VC6.0下有几条语句不能编译的去掉就能编译通过。
2015-9-26 15:40
0
雪    币: 290
活跃值: (41)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
5
const 与指针,找了一份最详细的教学......
===============================
看完之后总结:
int *pi;
会改变的值只有2 个-> *pi, pi -> const 要固定那个值
int const *pi; -> *pi 固定
int * const pi; -> pi 固定
看好长的教学,结果只有两种变化.......

===============================================
详细的教学:
1. const修饰普通变数和指标
    const修饰变数,一般有两种写法:
    const TYPE value;
    TYPE const value;
    这两种写法在本质上是一样的。它的含义是:const修饰的类型为TYPE的变数value是不可变的。
    对于一个非指标的类型TYPE,无论怎么写,都是一个含义,即value只不可变。
    例如:
        const int nValue; //nValue是const
        int const nValue; // nValue是const

但是对于指标类型的TYPE,不同的写法会有不同情况,例如:
    A. const char *pContent;
    B. char * const pContent;
    C. char const *pContent;
    D. const char* const pContent;

对于前三种写法,我们可以换个方式,给其加上括弧
    A. const (char) *pContent;
    B. (char*) const pContent;
    C. (char) const *pContent;

这样就一目了然。根据对于const修饰非指标变数的规则,很明显,A=C.

    - 对于A,C, const修饰的类型为char的变数*pContent为常量,因此,pContent的内容为常量不可变.
    - 对于B, 其实还有一种写法: const (char*) pContent;
      含义为:const修饰的类型为char*的变数pContent为常量,因此,pContent指针本身为常量不可变.
    - 对于D, 其实是A和B的混合体,表示指标本身和指标内容两者皆为常量不可变

总结:
    (1) 指针本身(pContent)是常量不可变
        (char*) const pContent;
        const (char*) pContent;

    (2) 指标所指向的内容(*pContent)是常量不可变
        const (char) *pContent;
        (char) const *pContent;

    (3) 两者都不可变
        const char* const pContent;

Syntax

const <variable name> [ = <value> ] ;
<function name> ( const <type>*<variable name> ;)
<function name> const;

Description

Use the const modifier to make a variable value unmodifiable.
Use the const modifier to assign an initial value to a variable that cannot be changed by the program.
Any future assignments to a const result in a compiler error.

A const pointer cannot be modified, though the object to which it points can be changed.
Consider the following examples.

   const float pi = 3.14;
   const maxint = 12345; // When used by itself, const is equivalent to int.
   char *const str1 = "Hello, world"; // A constant pointer
   char const *str2 = "Borland International"; // A pointer to a constant character string.

Given these declarations, the following statements are legal.

pi = 3.0; // Assigns a value to a const.
i = maxint++; // Increments a const.
str1 = "Hi, there!" // Points str1 to something else.

Using the const Keyword in C++ Programs

C++ extends const to include classes and member functions.
In a C++ class definition, use the const modifier following a member function declaration.
The member function is prevented from modifying any data in the class.
A class object defined with the const keyword attempts to use only member functions
that are also defined with const.
If you call a member function that is not defined as const,
the compiler issues a warning that the a non-const function is being called for a const object.
Using the const keyword in this manner is a safety feature of C.

Warning: A pointer can indirectly modify a const variable, as in the following:

*(int *)&my_age = 35;

If you use the const modifier with a pointer parameter in a function's parameter list,
the function cannot modify the variable that the pointer points to. For example,

int printf (const char *format, ...);

printf is prevented from modifying the format string.
2015-9-26 16:46
0
游客
登录 | 注册 方可回帖
返回
//