来自菜鸟的日程-------c语言基础篇11(结构体)

union sample
{
	short i;//2
	char ch;//1
	float f;//4
	union MyUnion
	{
		char ch;
	};
}s;
union uu
{
	int a;
	short b;
	int c;
};
union sample2
{
	char ch[10];//7+1
	int i;//4
	float f;//4
}s2;
struct sample4
{
	union sample5
	{
		char ch[7];
		int i;
		float f;
	}su;//8
	int a;//4
}s5;
int main()
{
	printf("%d\n",sizeof(sample4));
	s.i = 1;
	s.ch = 2;
	s.f = 12.5f;

	printf("%d\n",s.i);//10
	printf("%d\n",s.ch);//97
	printf("%f\n",s.f);//12.5
	printf("%d\n",sizeof(uu));
	return 0;
}

/*
typedef struct Student
{
	char name[10];
	int age;
}studnt;
void Show(studnt *p,int len)
{
	for(int i = 0;i < len;i++)
	{
		printf("%s,%d ",p[i].name,p[i].age);//下标访问
	}
	printf("\n");
}

*/

/内存对齐(向下对齐,向能整除的结构体中最大的字节数对齐) 前面字节数之和要整除下一个类型的字节数(当前类型的起始地址号要整除当前类型的字节数)
/

//C和C++的区别:
//C:空的结构体,大小不确定,报错
//c++:空的结构体,大小为1个字节

struct A
{
	char a;//1+3
	int b;//4
};//8
struct B
{
	char a;//1+1
	short b;//2
	int c;//4
};//8
//向前对齐
struct C
{
	char a;//1+3
	int b;//4
	short c;//2+6
	double d;//8
};//24
struct D
{
	char a;//1
	char  b;//1
	short c;//2
	int d;//4
};//8
struct  E
{
	char a;//1+1
	short c;//2
	char  b;//1+3
	int d;//4
};//12
//向后对齐 (考虑到结构体数组问题)
struct F
{
	int a;//4
	char c;//1+3
};//8

*/

/*

int main()
{
	printf("%d\n",sizeof(F));
	/*studnt arr[3] = {{"caocao",18},{"liubei",99},
	{"dema",8}};
	int len = sizeof(arr)/sizeof(arr[0]);
	Show(arr,len);
	return 0;
}

*/

/*

typedef struct A
{
	char a;
	int b;
	//struct A bb;//在当前结构体内,不能自引用
}Aa;
//   .:是否为变量         ->:本身是否为指针,本身是指针,就用指向符访问别的
struct B
{
	int i;//不能进行赋值
	struct A Aaa;
	struct A *p;
}Bb;

typedef struct Student
{
	char name[10];
	int age;
}studnt;

void Show(studnt stu)
{
	printf("%s,%d\n",stu.name,stu.age);
}
void Show2(studnt *pstu)//[]  ->
{
	printf("%s,%d\n",pstu->name,pstu->age);
	printf("%s,%d\n",(*pstu).name,(*pstu).age);
}
int main()
{
	studnt  stu = {"liubei",99};
	Show(stu);
	/*Bb.Aaa.a;
	Bb.i;
	Bb.p->a;
	Bb.p->b;*/
	/*Aa stu = {'a',12};
	printf("%c,%d\n",stu.a,stu.b);*/
//	return 0;
//}

例:

//struct Student
//{
//	char name[10];
//	int age;
//	char sex[5];
//}st = {"gao",18,"man"};
	/*
typedef struct Student
{
	char name[10];
	int age;
	char sex[5];
}st;

typedef define 区别是什么?
//typedef自定义类型
typedef int (*P)[3];
typedef int (*Pfun)(int,int);
typedef unsigned int Unint;
typedef int INT;
typedef int A[10];

*/

/*

int Max(int a,int b)
{
	return a+b;
}
typedef int *PP;//==》由这种变量提升为类型
#define P int*

int main()
{
	int i = 10;
	int i2 = 100;
	const  PP a = &i;//int *
	/*const int *p = &i;
	*p = 1000;
	p = &i2;*/
	//a = &i2;    //1
	//*a = 100;  //2

	//a = &i;
	//	b= &i;

	//P c,d;
	//c = &i;
	//d = &i;//int

	//A arr[11] = {1,2,3,4,5,6,7,8,9,0,10};
	//int arr2[10];//int []

	//Unint a = 10;
	//Student s1 = {"gao",18,"man"};
	//st = {"gao",18,"man"};
	//Pfun p = Max;
	//struct s = {};
	//return 0;
//}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值