2021/5/12 c语言---结构体

//结构体相当于自定义的数据类型

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct book{
	char name[10];
	char author[10];
};
//book是我们自定义的一种数据类型

int main(){
	struct book book[10];
	//类比 int arr[10];第一个book是自定义的数据类型,第二个book是变量名 
	strcpy(book[0].name, "西游记");
	//通过变量名.元素来取出结构体中的元素
	strcpy(book[0].author, "吴承恩");
	strcpy(book[1].name, "水浒传");
	strcpy(book[1].author, "施耐庵");
	int i;
	for(i = 0; i < 2; i++){
		printf("%s %s\n", book[i].name, book[i].author);
		
	} 
	
}
typedef的使用
typedef相当于给数据类型重命名
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int AAA;
typedef struct book{
	char name[10];
	char author[10];
}Book;

int main(){
	Book book[10];
	//相当于struct book book[10],只是用Book简写了struct book
	strcpy(book[0].name, "西游记");
	strcpy(book[0].author, "吴承恩");
	strcpy(book[1].name, "水浒传");
	strcpy(book[1].author, "施耐庵");
	int i;
	for(i = 0; i < 2; i++){
		printf("%s %s\n", book[i].name, book[i].author);
		
	} 
	
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//在声明结构体的同时创建结构体变量book[10]
struct book{
	char name[10];
	char author[10];
}book[10];


int main(){
	strcpy(book[0].name, "西游记");
	strcpy(book[0].author, "吴承恩");
	strcpy(book[1].name, "水浒传");
	strcpy(book[1].author, "施耐庵");
	int i;
	for(i = 0; i < 2; i++){
		printf("%s %s\n", book[i].name, book[i].author);
		
	} 
	
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct book{
	char name[10];
	char author[10];
}book[10];

//创建结构体的同时创建结构体变量,并为结构体重命名为a
typedef struct book a;
int main(){
	strcpy(book[0].name, "西游记");
	strcpy(book[0].author, "吴承恩");
	strcpy(book[1].name, "水浒传");
	strcpy(book[1].author, "施耐庵");
	int i;
	for(i = 0; i < 2; i++){
		printf("%s %s\n", book[i].name, book[i].author);
		
	} 
	
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct book{
	char name[10];
	char author[10];
}book[10];

//创建结构体的同时创建结构体变量,并为结构体重命名为a
typedef struct book Book;
int main(){
	Book *b=&book[0];
	//创建一个Book型的指针b,b存储结构体变量book[0]的地址
	strcpy(book[0].name, "西游记");
	strcpy(book[0].author, "吴承恩");
	strcpy(book[1].name, "水浒传");
	strcpy(book[1].author, "施耐庵");
	int i;
	for(i = 0; i < 2; i++){
		printf("%s %s\n", book[i].name, book[i].author);		
	} 
	printf("%s ",(*b).name);
	printf("%s",b->name);
	//(*b).name 和 b->name 等价
}
上一篇:学习周记 ( jQuery)


下一篇:关于React的Fiber的一些理解和总结