代码如下
#define _CRT_SECURE_NO_WARNINGS//vs报scanf警告
#include<stdio.h>
#include<string.h>
struct Student
{
char name[20];
char age;
char sex;
};
int main() {
struct Student st[] = { {"aaa",25,1},{"bbb",40,0}, {"ccc",30,1},{"ddd",34,1},{"eee",79,0} };//声明且初始化
int num = sizeof(st) / sizeof(st[0]);//计算出成员数量
printf("%d\n", sizeof(st));//这里输出结构体数组st所占的内存大小,这里是110byte,5个成员,每个成员22byte
for (int i = 0; i < num; i++)//冒泡排序,以年龄排序
{
for (int j = 0; j < num - i; j++)
{
if (st[j].age > st[j + 1].age)
{
st[num] = st[j];//这里我突发奇想把大的放置在数组后面,程序运行起来没问题,不知道实际上能不能这样???
st[j] = st[j + 1];
st[j + 1] = st[num];
}
}
}
scanf("%20s", st[5].name);//向数组添加新成员
scanf("%d", &st[5].age);
scanf("%d", &st[5].sex);
num = sizeof(st) / sizeof(st[0]);//问题来了!这里怎么还是5,不是6呢????那怎么输出新成员的信息呢???
printf("%d\n", sizeof(st));
for (int i = 0; i < num;i++)
{
printf("%s,%d,%d\n", st[i].name, st[i].age, st[i].sex);
}
return 0;
}