c语言高级

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值