结构体练习题19
#include<stdio.h>
struct student{//利用指针变量输出结构体数组
int num;
char *name;
char sex;
float score;
}stu[3] = { { 101, "zhao lei", 'M', 45 }, { 102, "sun hui", 'M', 62.5 },
{ 103, "li fang", 'F', 92.5 } };
void main(){
struct student *ps;
printf("num\tName\t\tSex\tScore\n");
for (ps = stu; ps < stu + 3; ps++)
printf("%d\t%s\t\t%c\t%f\n", ps->num, ps->name, ps->sex, ps->score);
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
struct student{//结构体数组的引用
int num;
char name[15];
int score[3];
}stu[] = { { 1, "david", { 80, 78, 92 } }, { 2, "lily", { 90, 84, 89 } }, { 3, "alice", { 79, 78, 96 } } };
void main(){
int i, j, num;
printf("input student's number:");
scanf("%d", &num);
for (i = 0; i < 3; i++)
if (num == stu[i].num)
break;
printf("name=%s\n", stu[i].name);
for (j = 0; j < 3; j++)
printf("%d ", stu[i].score[j]);
printf("\n");
system("pause");
}
#include<stdio.h>
struct student//指向结构体变量的指针使用
{
int num;
char *name;
char sex;
float score;
}stu1 = { 102, "zhangping", 'M', 78.5 },*pstu;
void main(){
pstu = &stu1;
printf("Number=%d\nName=%s\n", stu1.num, stu1.name);
printf("Sex=%c\nScore=%f\n\n", stu1.sex, stu1.score);
printf("Number=%d\nName=%s\n", (*pstu).num, (*pstu).name);
printf("Sex=%c\nScore=%f\n\n", (*pstu).sex, (*pstu).score);
printf("Number=%d\nName=%s\n", pstu->num, pstu->name);
printf("Sex=%c\nScore=%f\n\n", pstu->sex, pstu->score);
system("pause");
}

浙公网安备 33010602011771号