还有哪些方法让网站更加利于seo,asp flash网站源码,平面设计网上自学,东莞微网站建设报价面试的时候#xff0c;应该有遇到const相关的#xff0c;毕竟也是学习中的一个知识点#xff0c;看完我们这篇文章#xff0c;我觉得你应该可以在面试中完完全全的吃透const这个点。const和变量const uint32_t hello 3;编译的时候#xff0c;编译器就知道了 hello 这个变… 面试的时候应该有遇到const相关的毕竟也是学习中的一个知识点看完我们这篇文章我觉得你应该可以在面试中完完全全的吃透const这个点。const和变量const uint32_t hello 3;编译的时候编译器就知道了 hello 这个变量是不可以被修改了const其实也就是read only你只能读我的不能修改我。所以你要是试图修改这个变量的值编译器会告诉你clang-700.1.81:error: read-only variable is not assignable hello; ~~~~~^error: read-only variable is not assignable hello 92; ~~~~~ ^gcc-5.3.0:error: increment of read-only variable hello hello; ^error: assignment of read-only variable hello hello 92; ^C有一个特点只要const 在变量名之前就可以所以 const uint32_t i; 和uint32_t const i都是一样的。const uint32_t hello 3;uint32_t const hello 3;const 和函数参数我们有时候不希望函数体内的代码修改呢我们传入的参数我们就会这样做。我们看看下面这个代码#include stdio.h#include stdint.hvoid printTwo(uint32_t a, uint64_t b);void printTwo(const uint32_t a, const uint64_t b) { printf(%d %d\n, a, b);}int main(){ printTwo(12,23); return (1); }我们声明的时候没有加上const 但是我们定义函数的时候我们加上了const编译没有报错也成功执行了。声明只是一个虚张声势而已就好比严令禁止一样有些人还是继续作恶就好比。我解释得再深入一些我们知道函数形参非指针或者数组传过来的是一个拷贝函数体里面处理的是这个形参的拷贝而不是这个形参的实体就像影分身一样分身死了本体还在着呢。所以对分身的属性和本体的属性是不一样的。#include stdio.h#include stdint.hvoid printTwo(const uint32_t a, const uint64_t b);void printTwo(const uint32_t a, uint64_t b) { b 12; printf(%d %d\n, a, b);}int main(){ printTwo(12,23); return (1); }const 和数组#include stdio.h#include stdint.hconst uint16_t things[] {5, 6, 7, 8, 9};int main(){ things[1] 12; return (1); }编译一下就会报错8 12 G:\c\1.cpp [Error] assignment of read-only location things[1]const 和结构体例子#include stdio.h#include stdint.hstruct aStruct { int32_t a; uint64_t b;};const struct aStruct someStructA {.a 3, .b 4};int main(){ someStructA.a 12; return (1); }编译的时候13 16 G:\c\1.cpp [Error] assignment of member aStruct::a in read-only object我们也可以在结构体里面指明const这样就不会影响整个结构体而只影响里面的某一个参数了。const 和指针const 修饰指针变量指向的内容指针和const应该是最常见了毕竟指针一边就好比火车脱轨了开往了错误的方向。#include stdio.h#include stdint.huint64_t bob 42;uint64_t const *aFour bob;int main(){ *aFour 4; return (1); }一样的编译的时候还是会出错9 9 G:\c\1.cpp [Error] assignment of read-only location * aFourconst 修饰的是指针指向的内容说明指针执行的内容是read only的是不可改变。所以下面这个代码是没有问题的#include stdio.h#include stdint.huint64_t bob 42;uint64_t const *aFour bob;uint64_t b 32;int main(){ aFour b; return (1); }const 修饰指针变量同理如果const 修饰指针变量那就说明这个地址是不可变的。#include stdio.h#include stdint.huint64_t bob 42;uint64_t *const aFour bob;uint64_t b 32;int main(){ aFour b; return (1); }9 8 G:\c\1.cpp [Error] assignment of read-only variable aFour所以如果我们希望一个指针变量不能改变指向的内容也不能改变就应该这样写uint32_t bob 32;uint32_t const *const p bob;const 和#define的区别既然有了#define 还需要const 吗他们的恩恩怨怨我觉得今天应该要结束了即使没有结束也应该分出一个胜负了。•#define 是宏替换在预编译的时候确定•const 前面有数据类型会进行数据类型检查•#define 执行的宏名可以通过#undef来取消宏定义但是const修饰的变量保存在静态区会一直存在所以如果你有多个头文件声明同一个const 变量肯定会出现重复定义。但是你在两个头文件中#define 两个名字一样的宏名并不会有问题如果看了上面还不过瘾推荐一个const的文章因为没有获得作转载许可只给出链接C语言之 const 不变量扫码或长按关注回复「 加群 」进入技术群聊