MST

星途 面试题库

面试题:C语言结构体成员访问与操作(中等难度)

假设有如下结构体定义: ```c struct student { char name[20]; int age; float score; }; ``` 请编写一个函数,该函数接受一个`struct student`类型的指针作为参数,将该学生的年龄增加1,并修改成绩为原来的1.2倍,最后在主函数中创建一个`struct student`变量,调用这个函数并输出修改后的学生信息。
47.6万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>

// 定义结构体
struct student {
    char name[20];
    int age;
    float score;
};

// 定义修改学生信息的函数
void modifyStudent(struct student *stu) {
    stu->age += 1;
    stu->score *= 1.2;
}

int main() {
    struct student stu = {"Tom", 20, 85.5};

    modifyStudent(&stu);

    printf("Name: %s\n", stu.name);
    printf("Age: %d\n", stu.age);
    printf("Score: %.2f\n", stu.score);

    return 0;
}