定义结构体数组
定义结构体数组和定义结构体相似,在变量名表列声明它是数组即可。
例如
sturct student
{int num;
char name[20];
char sex;
char addr[30];
}sturct student stu[3];
或者直接定义一个结构体数组
sturct student
{int num;
.
.
.
}stu[3];
sturct
{int num;
.
.
.
}stu[3];
结构体数组的初始化一般形式是在定义数组后面加上“={初值表列}”。
结构体数组的应用
#include<stdio.h>
#include<string.h>
struct person
{
char name[20];
int count;
}leader[3] = {"li",0,"fun",0,"zhang",0};
void main()
{ int i, j;
char leader_name[20];
for(i = 0; i < 10; i++)
{
scanf("%s", leader_name);
for(j = 0; j < 3; j++)
if(strcmp(leader_name, leader[j].name) == 0)leader[j].count++;
}
printf("\n");
for(i = 0; i < 3; i++)
printf("%5s:%d\n",leader[i].name,leader[i].count);
}
ps:写代码时遇到的问题:
1. scanf 和 printf后的(“ ”)
2. 运行时语法没有错但是逻辑陷入了死循环
3. strcmp是比较字符串大小的函数,第46行的意思是比较leader_name的大小,如果相等,那这个name的数量加1。