一、初识 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,因此两者都不可以改变。
定义常变量不能写,只能读
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 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的地址
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的值,因此不可以
int * const cp4 = &ca;//error 与p = &ca原因一样,加个*号就可以改变ca的值,因此不可以
const int * const cp5 = &a;
const int * const cp6 = &ca;
两者定义是对的,但是都不可以改变它们的值和地址,上面有说明
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);
}
(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);
}
{
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);
#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;
}
return 0;
while(*str != '\0')
{
i++;
str++;
}
return i;
}
int main()
{
char *str={"sdghj"};
printf("%d\n",Mystrlen(str));
return 0;
}