常规
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Books{
char name[20];
char about[200];
double money;
} book={ "悲惨世界","雨果作品,简述了.......",16.01};
int main(void){
SetConsoleOutputCP(65001);
printf("书名:%d\n简介:%d\n价格 %f\n",book.name,book.about,book.money);
}
新增
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Books{
char name[20];
char about[200];
double money;
};
int main(void){
SetConsoleOutputCP(65001);
struct Books book1;
struct Books book2;
strcpy(book1.name,"笔墨纸砚");
strcpy(book1.about,"中国文房四宝缺一不可");
book1.money=12.56;
strcpy(book2.name,"上海1943");
strcpy(book2.about,"战争年代哪些事");
book2.money=10.22;
printf("书名:%s\n简介:%s\n价格 %f\n",book1.name,book1.about,book1.money);
printf("书名:%s\n简介:%s\n价格 %f\n",book2.name,book2.about,book2.money);
}
结构作为函数参数
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Books{
char name[20];
char about[200];
double money;
};
void console(struct Books book){//打印
printf("书名:%s\n简介:%s\n价格 %f\n",book.name,book.about,book.money);
}
int main(void){
SetConsoleOutputCP(65001);
struct Books book1;
struct Books book2;
strcpy(book1.name,"笔墨纸砚");
strcpy(book1.about,"中国文房四宝缺一不可");
book1.money=12.56;
strcpy(book2.name,"上海1943");
strcpy(book2.about,"战争年代哪些事");
book2.money=10.22;
console(book1);
console(book2);
}
指向结构的指针
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Books{
char name[20];
char about[200];
double money;
};
void console(struct Books *book){//打印
strcat(book->name,"---改变值");
printf("书名:%s\n简介:%s\n价格 %f\n",book->name,book->about,book->money);
}
int main(void){
SetConsoleOutputCP(65001);
struct Books book1;
struct Books book2;
strcpy(book1.name,"笔墨纸砚");
strcpy(book1.about,"中国文房四宝缺一不可");
book1.money=12.56;
strcpy(book2.name,"上海1943");
strcpy(book2.about,"战争年代哪些事");
book2.money=10.22;
console(&book1);
console(&book2);
}
结构体引用另一个结构体
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Student{
int age;
char name[20];
} ;
struct Books{
char name[20];
char about[200];
double money;
struct Student st; //包含其他的结构体 之前,本结构体要在被包含结构体之后定义
} ;
int main(void){
SetConsoleOutputCP(65001);
struct Books book1;
strcpy(book1.name,"笔墨纸砚");
strcpy(book1.about,"中国文房四宝缺一不可");
book1.money=12.56;
book1.st.age=1;
strcpy(book1.st.name,"小名");
printf("书名:%s\n简介:%s\n价格 %f\n小朋友年龄:%d\n小朋友的名字:%s",book1.name,book1.about,book1.money,book1.st.age,book1.st.name);
}
结构体嵌套
#include <stdio.h>
#include <windows.h>
#define MAX 10
struct Books{
char name[20];
char about[200];
double money;
struct Student{
int age;
char name[20];
} st;
} ;
int main(void){
SetConsoleOutputCP(65001);
struct Books book1;
strcpy(book1.name,"笔墨纸砚");
strcpy(book1.about,"中国文房四宝缺一不可");
book1.money=12.56;
book1.st.age=1;
strcpy(book1.st.name,"小名");
printf("书名:%s\n简介:%s\n价格 %f\n小朋友年龄:%d\n小朋友的名字:%s",book1.name,book1.about,book1.money,book1.st.age,book1.st.name);
}