//结构体的基本操作
//方法1:多次printf
#include <stdio.h>
typedef struct student//typedef就是将struct student改名为stu,省事
{
char name[20];//char类型后面的[]是给你将要输入的字符或者字符串预留空间
int age;
char sex[12];
int tele;
}stu;
int main()
{
stu stu1[5] = { {"zhaosan", 19, "男", 12123},//[5]是在stu stu1中放入5个元素
{"lisi", 20, "男", 10086},
{"wangwu", 22, "男", 12345},
{"muting", 20, "女", 12306},
{"wangxue", 19, "女", 12367}, };
printf("%s\t%d\t%s\t%d\n", stu1[0].name, stu1[0].age, stu1[0].sex, stu1[0].tele);//数组下标是从0开始
printf("%s\t%d\t%s\t%d\n", stu1[1].name, stu1[1].age, stu1[1].sex, stu1[1].tele);
printf("%s\t%d\t%s\t%d\n", stu1[2].name, stu1[2].age, stu1[2].sex, stu1[2].tele);
printf("%s\t%d\t%s\t%d\n", stu1[3].name, stu1[3].age, stu1[3].sex, stu1[3].tele);
printf("%s\t%d\t%s\t%d\n", stu1[4].name, stu1[4].age, stu1[4].sex, stu1[4].tele);
//输出类型要与你的顺序一一对应char--%s、int--%d
//\t是制表符,让输出的东西像表格一样板正;\n是换行符
return 0;
}
//方法2:利用for循环
#include <stdio.h>
typedef struct student//typedef就是将struct student改名为stu,省事
{
char name[20];//char类型后面的[]是给你将要输入的字符或者字符串预留空间
int age;
char sex[12];
int tele;
}stu;
int main()
{
stu stu1[5] = { {"zhaosan", 19, "男", 12123},//[5]是在stu stu1中放入5个元素
{"lisi", 20, "男", 10086},
{"wangwu", 22, "男", 12345},
{"muting", 20, "女", 12306},
{"wangxue", 19, "女", 12367}, };
int i = 0;//初始化
for (i = 0; i < 5; i++)
{
printf("%s\t%d\t%s\t%d\n", stu1[i].name, stu1[i].age, stu1[i].sex, stu1[i].tele);
}//输出类型要与你的顺序一一对应char--%s、int--%d
//\t是制表符,让输出的东西像表格一样板正;\n是换行符
return 0;
}
两种方法的输出结果相同,如下图所示: