const的世界

一、初识 const
 定义常变量不能写,只能读
 const int ca = 100; //只读
 const int cb;       //error要初始化,否则为任意值
                ca = 100; //不能再次赋值,因为不可以改变
二、了解 const
   (1)用来修饰变量,数据类型对于 const 而言是透明的
     1 类型 2 * 3 变量  其中1与2是等价的 const int(或者 int const 都可以),数字为放const的不同位置      
     const int  *cp = &a;
                 cp = &b;   //right
       *cp = 200; //error
    解:因为const修饰的是变量*cp的,所以*cp的内容是不可以改变的,而cp不受限制,就可以改变。
   (2)用来封锁直接修饰的内容,将改内容变成只读
     int  * const cp1 = &a;
               cp1 = &b; //error
              *cp1 = 200;//right
    解:同理,但这次const 修饰的是cp1,因此cp1的内容不可以改变,*cp1可以改变,不受限制。
   (3)扩充,但是太复杂,了解就好,不必深究
     int const * const ccp = &a;
                    ccp = &b   //error
       *ccp = 200;//error
     解:都错误个人理解是因为前面的const修饰的是*cpp,而后面的const修饰的是cpp,因此两者都不可以改变。
三,深交
   权力等同传递,不可以扩大,能缩小
     int      a = 10; //读 写
     int      b = 10;//读 写
    const ca = 10;//读
    const cb = 20;//读

     int *p = &a; //right p指向a是地址,a是自由的,无限制
            p = &ca;//不可以,因为如果对的话,*p就会改变ca的值,ca只可以读
   const int *cp1 = &a; //right a是自由的
                    cp1 = &b; //right const修饰的是*cp1
                    cp1 = &ca;//right cp1是自由的,哪怕加了*号,*cp1也是无法改变的,改变不了只能读的ca的值
   const int *cp2 = &ca;//right cp2指向ca的地址
   int * const cp3 = &a;//right cp3指向自由a的地址
   int * const cp4 = &ca;//error 与p = &ca原因一样,加个*号就可以改变ca的值,因此不可以

   const int * const cp5 = &a;
   const int * const cp6 = &ca;
   两者定义是对的,但是都不可以改变它们的值和地址,上面有说明
四、实践
(1)字符串的拷贝
#include <stdio.h>
void  Mystrcpy(char *des,const char *str) //const 为了使str不能改变
{
  int i;
  for(i = 0;str[i] != '\0';i++)
  {
   des[i] = str[i];
  }
   des[i] = '\0';
   printf("%s\n",des);
}
int main()
{
 char str[]={"asdf"};
 char des[10];
 Mystrcpy(des,str);
}
(2)字符的长度
#include <stdio.h>
#include <assert.h>
int Mystrlen(const char *str)
{
 int i=0;
 assert(str != NULL);
 if(str == NULL)
  return 0;
 
 while(*str != '\0')
 {
  i++;
  str++;
 }
 return i;
}
int main()
{
 char *str={"sdghj"};
 printf("%d\n",Mystrlen(str));
 return 0;
}
### C/C++ 中 `const` 和 `struct` 的定义与使用 #### 结构体的基础概念 在 C/C++ 编程语言中,结构体(`struct`)用于组合不同类型的数据项形成一个整体。通过这种方式可以创建复杂数据类型来表示现实世界对象或抽象实体[^1]。 ```cpp // 定义一个简单的student结构体 struct Student { char name[20]; int age; }; ``` #### 使用 typedef 简化结构体声明 为了简化结构体类型的命名,在实际开发过程中经常采用 `typedef` 关键字给自定义的结构体型赋予一个新的名字[^2]: ```cpp typedef struct { double real; double imag; } Complex; Complex c; // 创建了一个名为c的复数实例 ``` #### const 修饰符的作用范围 `const` 是一种限定符,用来指定某个变量、指针或者成员函数的结果不可被修改。当应用于结构体时,它可以作用于整个结构体对象或者是其内部特定字段上[^3]。 ##### 整个结构体作为常量 如果希望阻止对整个结构体实例的所有访问,则可以在声明该实例的时候加上 `const`: ```cpp const struct Point origin = {0, 0}; // 不允许改变origin的内容 ``` ##### 成员级别的只读属性 对于某些情况下只需要部分保护的情况,可以直接将单个成员设置成 `const`, 这样即使其他部分可变也不会影响这些受保护的部分: ```cpp struct Rectangle { float width; const float height; // 高度一旦初始化就不能再更改 }; Rectangle r = {.width=5,.height=8}; r.width = 6; // 合法操作 // r.height = 9; // 错误! 尝试修改const成员 ``` #### 函数参数传递中的应用 当把含有 `const` 的结构体传入函数时,可以通过引用的方式提高效率并防止意外修改原始数据: ```cpp void printStudent(const struct Student& stu){ std::cout << "Name:" << stu.name << ", Age:"<<stu.age<<std::endl; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值