MST

星途 面试题库

面试题:C语言结构体数组的基本操作

假设有一个结构体如下:struct Student { char name[20]; int age; float score; }; 请编写代码实现创建一个包含5个学生信息的结构体数组,并初始化这些学生的信息,然后遍历该数组,打印出所有年龄大于20岁且分数大于80分的学生的名字。
36.5万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>

struct Student {
    char name[20];
    int age;
    float score;
};

int main() {
    struct Student students[5] = {
        {"Alice", 22, 85.5},
        {"Bob", 19, 78.0},
        {"Charlie", 25, 88.0},
        {"David", 21, 75.0},
        {"Eve", 23, 90.0}
    };

    for (int i = 0; i < 5; i++) {
        if (students[i].age > 20 && students[i].score > 80) {
            printf("%s\n", students[i].name);
        }
    }

    return 0;
}