MST

星途 面试题库

面试题:C语言结构体指针的基础运用

假设有一个结构体定义如下: ```c struct Student { char name[20]; int age; float score; }; ``` 请编写一段代码,使用结构体指针来初始化一个`Student`结构体变量,并输出该变量的信息。
26.3万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>

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

    struct Student student;
    struct Student *ptr = &student;

    // 使用结构体指针初始化
    sprintf(ptr->name, "Tom");
    ptr->age = 20;
    ptr->score = 85.5f;

    // 输出结构体变量信息
    printf("Name: %s\n", ptr->name);
    printf("Age: %d\n", ptr->age);
    printf("Score: %.2f\n", ptr->score);

    return 0;
}