#include
#include
struct BOOK0{
int id;
char name[128];
float price;
}book_1;
typedef struct BOOK1{
int id;
char name[128];
float price;
}BOOK2;
int main()
{
struct BOOK0 book_0;
BOOK2 book_2;
}
/*总结:
struct BOOK0{...}; 声明结构体类型
struct BOOK0 book_0;或者struct BOOK0{...}book_1; 定义结构体变量book
typedef struct BOOK1{...}BOOK2; 新的结构体名称BOOK2。定义结构体变量 BOOK2 book_2;
*/
#include
#include
typedef struct BOOK1{
int id;
char name[128];
float price;
}BOOK2;
int main()
{
BOOK2 book_2;
book_2.id=1;
strcpy(book_2.name,"abc");
book_2.price=11.11;
printf("id=%d,name:%s,price=%.2f\n\n",book_2.id,book_2.name,book_2.price);
BOOK2 book_3;
BOOK2 *p=&book_3;
p->id =2; //(*p).id=2;
strcpy(p->name,"qwe");
p->price=22.22;
printf("id=%d,name:%s,price=%.2f\n\n",p->id,p->name,(*p).price);
}
/*编译结果:
id=1,name:abc,price=11.11
id=2,name:qwe,price=22.22
*/
/*总结:
通过结构体变量名访问: 结构体变量名.成员变量名
通过指针访问: 指针名->成员变量名,(*指针名).成员变量名(不常用)
结构体变量首地址: &book_3
“.”的优先级高于“*”,取内容的时候一定要加()
在结构体中使用“->”来访问结构体指针变量中的数据。
“.”一般用于结构体变量,“->”只适用于结构体指针。
*/
#include
#include
typedef struct BOOK1{
int id;
char name[128];
float price;
}BOOK2;
int main()
{
BOOK2 book_0;//随机值
BOOK2 book_1={11,"aaa"};//默认为0
BOOK2 book_2;
book_2.id=11;
//char buff[128];
//gets(book_2.name);
//strcpy(book_2.name,buff);
strcpy(book_2.name,"bbb");
book_2.price=22.22;
BOOK2 book_3={33,"ccc",33.33};//初始化赋值
BOOK2 book_4;
book_4=book_1;//整体赋值
printf("id=%d, name:%s, price=%.2f\n\n",book_0.id,book_0.name,book_0.price);
printf("id=%d, name:%s, price=%.2f\n\n",book_1.id,book_1.name,book_1.price);
printf("id=%d, name:%s, price=%.2f\n\n",book_2.id,book_2.name,book_2.price);
printf("id=%d, name:%s, price=%.2f\n\n",book_3.id,book_3.name,book_3.price);
printf("id=%d, name:%s, price=%.2f\n\n",book_4.id,book_4.name,book_4.price);
}
/*编译结果:
id=28, name:, price=0.00
id=11, name:aaa, price=0.00
id=22, name:bbb, price=22.22
id=33, name:ccc, price=33.33
id=11, name:aaa, price=0.00
*/
/*总结:
未赋值为随机值,未赋值部分为0,初始化时可以整体赋值,同类型结构体变量之间可以在整体赋值
*/
#include
#include
struct AUTHOR{
char author_name[128];
int age;
};
typedef struct BOOK1{
int id;
char name[128];
struct AUTHOR author;
float price;
}BOOK2;
int main()
{
BOOK2 book_0;
book_0.id=0;
strcpy(book_0.name,"qwer");
strcpy(book_0.author.author_name,"QWER");
book_0.author.age=22;
book_0.price=33.33;
printf("id=%d, name:%s, auther_name:%s, auther_age=%d, price=%.2f\n\n"
,book_0.id
,book_0.name
,book_0.author.author_name,book_0.author.age
,book_0.price);
BOOK2 book_1={1,"aaa","AAA",11,11.11};//初始化
printf("id=%d, name:%s, auther_name:%s, auther_age=%d, price=%.2f\n\n"
,book_1.id
,book_1.name
,book_1.author.author_name,book_1.author.age
,book_1.price);
BOOK2 book_2=book_0;//结构体变量之间整体赋值
printf("id=%d, name:%s, auther_name:%s, auther_age=%d, price=%.2f\n\n"
,book_2.id
,book_2.name
,book_2.author.author_name,book_2.author.age
,book_2.price);
BOOK2 book_3;//scanf输入赋值
scanf("%d%s%s%d%f"
,&book_3.id
,book_3.name
,book_3.author.author_name,&book_3.author.age
,&book_3.price);
printf("id=%d, name:%s, auther_name:%s, auther_age=%d, price=%.2f\n\n"
,book_3.id
,book_3.name
,book_3.author.author_name,book_3.author.age
,book_3.price);
}
/*编译结果:
id=0, name:qwer, auther_name:QWER, auther_age=22, price=33.33
id=1, name:aaa, auther_name:AAA, auther_age=11, price=11.11
id=0, name:qwer, auther_name:QWER, auther_age=22, price=33.33
33 ccc CCC 33.33
id=33, name:ccc, auther_name:CCC, auther_age=33, price=0.33
*/
/*总结:
未赋值为随机值,未赋值部分为0,初始化时可以整体赋值,同类型结构体变量之间可以在整体赋值
scanf时,id、age、price使用取址&
*//*结构体类型空间大小*/
#include
#include
typedef struct students{
int id;
char name[10];
float score;
}student;
int main()
{
student i;
printf("sizeof(int)+sizeof(char)*10+sizeof(float)=%d\n",sizeof(int)+sizeof(char)*10+sizeof(float));
printf("i=%d",sizeof(i));
printf("struct students = %d\n",sizeof(struct students));
}
/*编译结果:
sizeof(int)+sizeof(char)*10+sizeof(float)=18
i=20struct students = 20
*/
/*总结:
4 10 2 4
———— + ———— + ————— + ———————— = 20
int(4) char(1) 结构体空洞 float(4)
LIUNX操作系统下:
按照4字节数据对齐原则。 当排到该成员时,其前面已开辟的空间字节数必须是该成员类型所占字节数的整数倍,如果不够则补齐,依次向后类推。 整体空间是4的整数倍。
WINDOW操作系统下:
按照数据对齐原则。 当排到该成员时,其前面已开辟的空间字节数必须是该成员类型所占字节数的整数倍,如果不够则补齐,依次向后类推。 整体空间是结构体中最大成员数据类型的整数倍。
*/