18.7.29 (t' n' b' l' y')

#include<stdio.h>

int main()
{
    const int p;//只读变量,不能通过p修改对应内存的值。
    int * const q;//指针q的指向不能被修改
    int const *z;
    const int *a;//和上方int const *z相等,即不能指针指向地址内的值不能被修改
    const int *const b;// b++和(*b)++都不能被修改。
    return 0;
}

********************结构体*********************

#结构体
声明一个结构体类型的一般形式为:
struct 结构体名 {成员列表};

#打印结构体只能 挨个 打印。

结构定义:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct student
{
	char name[20];
	int number;
	char sex[20];
};

int main()
{
	struct student a1 = {"zq",1,"male"};
	struct student a2;
	
	struct student *a3 = (struct student*)malloc(sizeof(struct student));
	strcpy(a3 -> name,"dog");
	a3 -> number = 3;
	strcpy(a3 -> sex,"male");
	scanf("%s%d%s",a2.name,&a2.number,a2.sex);
	printf("%s %d %s",a3->name,a3->number,a3->sex);
	printf("%s %d %s\n",a1.name,a1.number,a1.sex);
	printf("%s %d %s\n",a2.name,a2.number,a2.sex);
	
	return 0;

eg

#include<stdio.h>

typedef struct student
{
	int num;
	char name[32];
	char sex;
	int age;
}stu;

int main()
{
	int i;
	stu *a[5] = {0};
	for(i= 0; i< 5;i++)
	{
		a[i] = (stu*)malloc(sizeof(stu));
		scanf("%d %s%c %d",&a[i]->num,a[i]->name,&a[i]->sex,&a[i]->age);
	}
	printf("\n");
	for(i = 0;i<5 ;i++)
	{
		printf("%d %s %c %d\n",a[i].num,a[i].name,a[i].sex,a[i].age);
	}
	return 0;
}

************************联合体************************

#union  联合体
C:
#include<stdio.h>

union test //所有成员共享同一段内存(只为最长字节成员分配空间)
{
    char a;
    int b;
    long c;
};//只为 int /long 分配4个字节。

#include<stdio.h>

union test
{
	char a;
	char b;
};

int main()
{
	int len = sizeof(union test);
	printf("%d\n",len);

	union test T;
	scanf("%d",&T.a);
	printf("%d\n",T.b);
}


*********************内存管理********************

4G虚拟内存
1G:内核态
                { 数据段:全局变量、static静态变量
3G用户态 {代码段:代码、常量(只读)
                {堆空间:malloc:、free
    {栈:局部变量(形参)

数据段还可分为BSS段和数据段。

char a[]="122344"//a++是错的,因为数组名a是个常指针; (*a)++正确;
char *b = "12345";//只读的 b++正确。(*b)++是错的。

堆和栈区别:
栈:操作系统管理,申请释放都是操作系统完成
堆:用户管理,申请和释放由用户完成 
       申请:malloc      释放:free

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int global = 0;//在数据段
char *p1; //在数据段

int main()
{
	int a;//在 栈空间
	char s[] = "abcd";//在 栈空间
	char *p2;//在 栈空间
	char *p3 = "123456789";//在 栈空间里一个指针变量指向一个在代码段的字符串常量。
	static int c = 0;
	p1 = (char*)malloc(100); //在堆空间 分配了100字节 给p1.

	strcpy(p1,"123456789");//在 代码段里的字符串常量给了堆空间里的p1.

	return 0;
}

#字节序
大端:高字节放在低地址
小段:高字节放在高地址
 

#include<stdio.h>

int main()
{
	int a = 1, res;

	res = ( (a & 0x000000ff) << 24) | ( (a & 0x0000ff00) << 8)|
		  ( (a & 0x00ff000) >> 8)|( (a & 0xff000000)>>24);
	printf("%d\n",res);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41481342/article/details/81273919
Y/N