结构体变量初始化
#include<string.h>
#include<stdio.h>
struct Student
{
int id;
char name[20];
char gender;
char class[40];
char address[200];
}stu1 = { 1001, "C C++",'M',"LDCI0701","New York" };
int main()
{
struct Student stu2 = { 1002,"Ha H@",'F',"LDCI0702","CHINA" };
printf("Student sut1:%d, ", stu1.id);
printf("%s, ", stu1.name);
printf("%c, ", stu1.gender);
printf("%s, ", stu1.class);
printf("%s\n", stu1.address);
printf("Student sut2:%d, ", stu1.id);
printf("%s, ", stu2.name);
printf("%c, ", stu2.gender);
printf("%s, ", stu2.class);
printf("%s\n", stu2.address);
return 0;
}
- 演示
结构体数组元素引用实例
#include<stdio.h>
#pragma warning(disable:4996);
struct STU
{
char name[20];
int age;
char sex;
char num[20];
};
void OutputSTU(struct STU stu[5]);
int main(void)
{
int i;
struct STU stu[5];
for (i = 0; i < 5; ++i)
{
printf("请输入第%d个学生的信息:", i + 1);
scanf("%s%d %c%s", stu[i].name, &stu[i].age, &stu[i].sex, stu[i].num);
}
OutputSTU(stu);
return 0;
}
void OutputSTU(struct STU stu[5])
{
struct STU stumax = stu[0];
int j;
for (j = 1; j < 5; j++)
{
if (strcmp(stumax.num, stu[j].num) < 0)
{
stumax = stu[j];
}
}
printf("学生姓名:%s 学生年龄:%d 学生性别:%c 学生学号:%s\n", stumax.name, stumax.age, stumax.sex, stumax.num);
}
- 演示
- 学号最大的通过 outputSTU函数输出
用结构体指针输出结构体数组的各项元素成员
#include<stdio.h>
#pragma warning(disable:4996);
struct stu
{
int num;
char* name;
char sex;
float score;
} *ps, boy[5] =
{
{101,"Wang yi", 'M', 45},
{102,"Wang er", 'F', 35},
{103,"Wang sa", 'M', 55.5},
{104,"Wang si", 'F', 65.5},
{105,"Wang wu", 'M', 75},
};
int main()
{
printf("No\tName\t\tSex\tScore\t\n");
for (ps = boy; ps < boy + 5; ps++)
printf("%d \t%s \t%c \t%f \t\n", ps->num, ps->name, ps->sex, ps->score);
return 0;
}
- 演示
用结构体变量作为参数传递的示例
#include<stdio.h>
#define FORMAT "%d\n%s\n%f\n%f\n%f\n"
struct Student
{
int num;
char cName[20];
float fScore[3];
};
int main()
{
void print(struct Student);
struct Student stu;
stu.num = 1001;
strcpy(stu.cName, "JiaZhiJie");
stu.fScore[0] = 77.5;
stu.fScore[1] = 88;
stu.fScore[2] = 98.5;
print(stu);
return 0;
}
void print(struct Student stu)
{
printf(FORMAT, stu.num, stu.cName, stu.fScore[0], stu.fScore[1], stu.fScore[2]);
}
- 演示
用结构体变量的成员作为参数传递的示例
#include<stdio.h>
struct Student
{
int num;
char cName[20];
float fScore[3];
};
void print(char* cName)
{
printf("cName: %s\n", cName);
}
int main()
{
struct Student STU = { 1001,"ZhiZhen",88.5,59,77 };
print(STU.cName);
return 0;
}
- 演示
用指向结构体变量的指针作为参数传递示例
#include<stdio.h>
#define FORMAT "%d\t%s\t%f\t%f\t%f\n"
struct Student
{
int num;
char cName[20];
float fScore[3];
}stu = { 1001, "ZhiZhen ",55.5,66.5,99};
int main()
{
void print(struct Student*);
struct Student* pt = &stu;
print(pt);
return 0;
}
void print(struct Student* p)
{
printf(FORMAT,p->num,p->cName,p->fScore[0],p->fScore[1],p->fScore[2]);
}
- 演示
用结构体数组元素的成员作为参数传递示例
#include<stdio.h>
struct Student
{
int num;
char name[20];
char sex;
int age;
float score;
};
int main()
{
void print(int num);
struct Student stu[3] = { {1001,"ZhiZhen",'M',39,47},
{1002,"WangAnShi",'M',40,99},
{1003,"LiBai",'M',41,87} };
print(stu[0].num);
}
void print(int num)
{
printf("num: %d\n", num);
}
- 演示
用指向结构体数组的指针作为参数传递示例
#include<stdio.h>
#define FORMAT "%d\t%s\t%c\t%d\t%f\n"
struct Student
{
int num;
char name[20];
char sex;
int age;
float score;
};
int main()
{
void print(struct Student* p);
struct Student stu[3] = { {1001,"ZhiZhen", 'M',10,47},
{1002,"WangAn", 'W',22,99},
{1003,"LiBai", 'M',11,87} };
struct Student* pt = stu;
print(pt);
}
void print(struct Student* p)
{
for (int i = 0; i < 3; i++)
printf(FORMAT, p[i].num, p[i].name, p[i].sex, p[i].age, p[i].score);
}
- 演示