C语言CONST

来源:互联网 发布:剑三御姐捏脸数据 编辑:程序博客网 时间:2024/05/16 05:22


关于CONST的学习:


主要分为两点:


1.const的修饰的格式:


const使用的基本形式: const type m;限定m不可变。当然m可以使指针或者其他的类型

看一下代码的例子:

int const n1 = 19;int const n2 = 29;//这个情况不可以修改指针指向的值:*p1 = 1;这个就是错误int const *p1,*p2 ;p1 = &n1;p2 = &n2;p1 = p2;//*p1 = 1;//这个情况不可以修改指针指向的值:*p3 = 33;这个就是错误const int *p3;p3 = &n1;p3 = &n2;

细化说明:

      const int nValue; //nValue是const

      const char *pContent; //*pContent是const, pContent可变
      const char* const pContent; //pContent和*pContent都是const

其中注意的是  const修饰在前面和后面都是一样的效果哈。。。。


2.const出现的一行语句中出现的多种情况:

1)

  int const * p1,p2;
  p2是const;(*p1)是const.


2)
  int const * const p1,p2;

  p2是const,是前一个const修饰的,*p1也被前一个const修饰,而p1被后一个const修饰。


3)
  int * const p1,p2;

  p1是const,(* const p1)是整体,所以const不修饰p2。p2就是普通的变量







关于CONST的学习:


主要分为两点:


1.const的修饰的格式:


const使用的基本形式: const type m;限定m不可变。当然m可以使指针或者其他的类型

看一下代码的例子:

int const n1 = 19;int const n2 = 29;//这个情况不可以修改指针指向的值:*p1 = 1;这个就是错误int const *p1,*p2 ;p1 = &n1;p2 = &n2;p1 = p2;//*p1 = 1;//这个情况不可以修改指针指向的值:*p3 = 33;这个就是错误const int *p3;p3 = &n1;p3 = &n2;

细化说明:

      const int nValue; //nValue是const

      const char *pContent; //*pContent是const, pContent可变
      const char* const pContent; //pContent和*pContent都是const

其中注意的是  const修饰在前面和后面都是一样的效果哈。。。。


2.const出现的一行语句中出现的多种情况:

1)

  int const * p1,p2;
  p2是const;(*p1)是const.


2)
  int const * const p1,p2;

  p2是const,是前一个const修饰的,*p1也被前一个const修饰,而p1被后一个const修饰。


3)
  int * const p1,p2;

  p1是const,(* const p1)是整体,所以const不修饰p2。p2就是普通的变量




原创粉丝点击