#C语言修饰符
const``
//等价
const char *p;//一般是这样
//读取的是只读的 字符串 "hello world"
char const *p;
//等价
char* const p;
//指向的固定地址 硬件资源 LCD
char *p const;
const char *const p;
//地址和内容都不能变 ROM资源
#include <stdio.h>
int main(void)
{
char *p = "hello world";//""双引号,理解为const char *
printf("the a is %x\n",*p);
*p = 'a';
printf("the %s\n",p);
}
上面代码会出现段错误,因为字符串常量无法修改,不能被非法访问
#include <stdio.h>
int main(void)
{
char *p = "hello world";
char *p2;
char buf[] = {"hello world"};
printf("the a is %x\n",*p);
p2 = buf;
*p2 = 'a';
printf("the %s\n",p2);
}
buf是一个变量,可以被修改
内存属性:
1,内存操作的大小
2,内存的变化型,可写可读问题
valatile
防止优化,指向内存地址
两者不一样
int (*p)[6];//
int *p[6];
```c
struct
在这里插入代码片struct abc{
int a;
char b;
};
int main(int argc, char **argv)
{
struct abc ab;
printf("%ld\n",sizeof(ab));
return 0;
}
按道理来说,struct abc的大小等于5个字节,但是实际运行得出的值却为8,
这里主要是牺牲一点空间换取时间的效率,字节对齐
#include <stdio.h>
struct abc{
int a;
short e;
char b;
};
struct abc2{
char b;
int a;
short e;
};
int main(int argc, char **argv)
{
struct abc ab;
struct abc2 ab1;
printf("%lu\n",sizeof(ab));
printf("%lu\n",sizeof(ab1));
return 0;
}
看似成员一样但所需内存不一样
第一个为8
第二个为12