1. 程式人生 > 實用技巧 >7.4_結構體_結構體指標作為函式引數

7.4_結構體_結構體指標作為函式引數

#include <stdio.h>
//結構體指標作為函式引數

struct Student{
    unsigned int no;
    char name[16];
    float score;
};

//輸入學生資訊(引數:陣列首地址)
void input(struct Student *p);

//尋找6個學生中成績最高的學生(引數:陣列首地址)
void find_max(struct Student *p);

int main() {

    struct Student stu[6];

    input(stu);

    find_max(stu);

    
return 0; } void input(struct Student *p){ for(int i=0; i<6; i++){ printf("輸入第%d個學生的資訊【編號 姓名 成績】:", i+1); scanf("%d %s %f", &p[i].no, p[i].name, &p[i].score); } } void find_max(struct Student *p){ //假設第一個元素的成績最高 struct Student max = *p; p++; for(int i=0
; i<6; i++){ if(p->score > max.score){ max = *p; } p++; } printf("成績最高的學生資訊:\n"); printf("編號:%d 姓名:%s 成績:%.2f\n", max.no, max.name, max.score); }