结构体
1.产生与意义
2.类型描述
struct 结构体名
{
数据类型 成员1;
数据类型 成员2;
.............;
};
3.嵌套定义
4.定义变量(变量,数组,指针),初始化及成员引用
成员引用 : 变量名. 成员名
指针->成员名
(*指针). 成员名
5.结构体占用内存空间大小
#include<stdlio.h>
#include<stdlib.h>
#define NAMESIZE 32
//结构体定义一般在函数外
//命名习惯 _st 结构体
struct node_st
{
int i,j;
float f;
char ch;
};//只是描述,并不占用空间
#if 0
struct birthday_st
{
int year;
int month;
int day;
};
#endif
struct student_st
{
int id;
char name[NAMESIZE];
//int year,moth,day;
//struct birthday_st birth;
//直接整体拿过来 在结构体里面定义 --看自己习惯
struct birthday_st
{
int year;
int month;
int day;
}birth;
int math;
int chinese;
};
int main()
{
//TYPE NAME = VALUE;
/*
struct node_st a = {123,456.789,'a'};
a.i = 112233;
printf("%d %f %c\n",a.i,a.f,a.ch);
*/
int i;
struct student_st stu = {10011,"Alan",{2024,11,11},98,97};
struct student_st *p = &stu;
struct student_st arr[2] = {{10011,"Alan",{2024,11,11},98,97},{10012,"Jahn",{2024,12,11=2},90,80}};
p = &arr[0];
//struct student_st stu = {.math = 98,.chinese = 97};//给部分结构体中的元素赋值
//printf("%d %s %d-%d-%d %d %d\n",stu.id,stu.name,stu.birth.year,stu.birth.month,stu.birth.day,stu.math,stu.chinese);
for(i = 0;i < 2;i++,p++)//p = p+1 p指向什么类型,跳几个字节
{
printf("%d %s %d-%d-%d %d %d\n",p->id,p->name,p->birth.year,p->birth.month,p->birth.day,p->math,p->chinese);
}
exit(0);
}
占用内存的大小
#include<stdlio.h>
#include<stdlib.h>
#define NAMESIZE 32
//结构体定义一般在函数外
//命名习惯 _st 结构体
struct simp_st
{
int i;
float f;
char ch;
};//地址对齐 ( 字节对齐 )
struct simp_st
{
int i;
float f;
char ch;
}__attribute__((packed));//可以不对齐,在网络传输中。
struct birthday_st
{
int year;
int month;
int day;
};
struct student_st
{
int id;
char name[NAMESIZE];
struct birthday_st birth;
int math;
int chinese;
};
int main()
{
struct simp_st a;
struct simp_st *p = &a;
printf("sizeof(point) = %d\n",sizeof(p));//8
printf("sizeof(struct) = %d\n",sizeof(a));//12
exit(0);
}
函数传参(值 , 地址)
#include<stdlio.h>
#include<stdlib.h>
#define NAMESIZE 32
//结构体定义一般在函数外
//命名习惯 _st 结构体
struct simp_st
{
int i;
float f;
char ch;
};//地址对齐 ( 字节对齐 )
struct birthday_st
{
int year;
int month;
int day;
};
struct
{
inti;
char ch;
float f;
}a = {,,},b = {,,},c,*p,*q;//结构体可以没名字 但是在声明是必须定义
struct student_st
{
int id;
char name[NAMESIZE];
struct birthday_st birth;
int math;
int chinese;
};
void func(struct simp_st *b)
{
printf("%d\n",sizeof(b));
}
int main()
{
struct simp_st a;
struct simp_st *p = &a;
//func(a);//--->func(a.i,a.ch,a.f);此时传参的开销特别大,临时建立的一个变量
func(p);
exit(0);
}
微型学生管理系统
#include<stdlio.h>
#include<stdlib.h>
#include<string.h>
#define NAMESIZE 32
//结构体定义一般在函数外
struct student_st
{
int id;
char name[NAMESIZE];
int math;
int chinese;
};
void stu_set(struct student_st *p,const struct student_st *q)
{
/*
p->id = 1011;
strncpy(p->name,"Alan",NAMESIZE);
p->math = 90;
p->chinese = 98;
*/
*p = *q;//结构体之间可以直接赋值
}
void stu_show(struct student_st *p)
{
printf("%d %s %d %d\n",p->id,p->name,p->math,p->chinese);
}
void stu_changename(struct student_st*p,const char *newname)
{
strcpy(p->name,newname);
}
void menu(void)
{
printf("1 set\n2 change name \n3 show\n");
printf("please enter the num(q for quit):")
}
int main()
{
struct student_st stu,tmp;
char newname[NAMESIZE];
int choice;
int ret;
do
{
menu();
ret = scanf("%d",&choice);
if(ret! = 1)
break;
switch(choice)
{
case 1:
printf("Please enter for the stu[id name math chinese]:");
scanf("%d%s%d%d",&tmp.id,tmp.name,&tmp.math,&tmp.chinese)
stu_set(&stu,&tmp);
break;
//stu_show(&stu);
case 2:
printf("Please enter input a newname:");
scanf("%s",newname);
stu_changename(&stu,newname);
break;
//stu_show(&stu);
case 3:
stu_show(&stu);
break;
defult:
exit(1);
}
sleep(1);
}while(1);
exit(0);
}