MST
星途 面试题库

面试题:C语言结构体指针中等难度题

假设有如下结构体定义:struct student { char name[20]; int age; float score; }; 请编写一个函数,接收一个指向struct student结构体的指针,打印出该学生的信息。
10.6万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>

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

// 定义打印学生信息的函数
void printStudentInfo(struct student *stu) {
    printf("Name: %s\n", stu->name);
    printf("Age: %d\n", stu->age);
    printf("Score: %.2f\n", stu->score);
}

你可以在main函数中这样调用这个函数:

int main() {
    struct student stu = {"Alice", 20, 85.5};
    printStudentInfo(&stu);
    return 0;
}