目录
[2.const 修饰指针](#2.const 修饰指针)
[1.const int*p=&m;](#1.const int*p=&m;)
[2. int* const p=&m;](#2. int* const p=&m;)
[3. int const *p=&m;](#3. int const *p=&m;)
[4. int const *const p=&m;](#4. int const *const p=&m;)
[2.关于signed char和unsigned char的数据取值范围和循环](#2.关于signed char和unsigned char的数据取值范围和循环)
[1.signed char和char数据的取值范围及循环](#1.signed char和char数据的取值范围及循环)
[2.unsigned char类型数据的取值范围和循环](#2.unsigned char类型数据的取值范围和循环)
4.应用

接下来的日子会顺顺利利,万事胜意,生活明朗-----------林辞忧
前言
在c语言中常常会遇到一些用const修饰的指针或变量,而且const位置不同表示含义不同,容易出现错误,计算char类型数据时常常会忘记char类型的取值范围,循环等导致出错,还有用结构体来实现位段,用来节省空间,接下来将逐一介绍
一:const修饰指针
1.const修饰变量
我们知道一个变量是可以修改的,如果将变量的地址给一个指针变量,可以通过指针改变变量的值。那么如果我们不希望改变变量的值,那么就将要用到const来修饰
#include<stdio.h>
int main()
{
const int m=0;//此时m具有了常属性,不能被修改,但本质上那还是变量,为常变量
//但在c++中const修饰的变量为常量
m=20;//这种写法是错误的
int *p=&m;
*p=20;//这种是被允许的
const修饰变量是不能直接通过修改变量的值,但可以通过间接的方法如通过指针来修改
}
2.const 修饰指针
const修饰指针有四种形式分别为
const int*p=&m;
int* const p=&m;
int const *p=&m;
int const *const p=&m;
我们依次介绍举例说明
1.const int*p=&m;

2. int* const p=&m;

3. int const *p=&m;

4. int const *const p=&m;

5.总结
const修饰指针变量的时候,放在*的右边,限制的指针变量本身,指针变量不能再指向其他变量,但可以通过指针变量来修改指针变量所指向的内容
const修饰指针变量的时候,放在*的左边,限制的是指针所指向的内容,不能通过指针来修改指向的内容,但可以修改指针变量本身,指针变量能指向其他变量
既放左边又放右边时,限制的指针变量本身和指针所指向的内容,指针变量不能再指向其他变量,不能通过指针来修改指向的内容
总之一句话为:左定值有定向
二:关于计算中char类型数据的特殊转换
1.关于char类型数据的整型提升
2.关于signed char和unsigned char的数据取值范围和循环
1.signed char和char数据的取值范围及循环

2.unsigned char类型数据的取值范围和循环

3.关于char类型数据计算中的经典易错例题
1.例题一
2.例题二

3.例题三

4.例题四

5.例题五

6.例题六

三:结构体实现位段
1.位段的介绍
1.位段的成员必须是 int 、 unsigned int 或 signed int ,char
2.位段的成员名后边有⼀个冒号和⼀个数字
2.位段的内存分配
-
位段的成员可以是 int unsigned int signed int 或者是 char 等类型
-
位段的空间上是按照需要以4个字节( int )或者1个字节( char )的⽅式来开辟的。
-
位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使⽤位段。
4.申请到的一块内存中,从左往右使用还是从右往左使用是不确定的;剩余的空间,不足一个成员使用时,是浪费还是继续使用是不确定的
5.栈是从高地址向低地址使用的,数组是随着下标的增长,由低地址向高地址使用
如:struct S
{
char a:3;
char b:4;
char c:5;
char d:4;
};
struct S s = {0};
s.a = 10;
s.b = 12;
s.c = 3;
s.d = 4;
在开辟空间时

3.位段使用的注意事项
位段的⼏个成员共有同⼀个字节,这样有些成员的起始位置并不是某个字节的起始位置,那么这些位
置处是没有地址的。内存中每个字节分配⼀个地址,⼀个字节内部的bit位是没有地址的。
所以不能对位段的成员使⽤&操作符,这样就不能使⽤scanf直接给位段的成员输⼊值,只能是先输⼊
放在⼀个变量中,然后赋值给位段的成员。
4.应用
在一些网络数据包中被广泛应用