{再び}C言語 構造体 関数

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
kssss

{再び}C言語 構造体 関数

#1

投稿記事 by kssss » 7年前

4人の成績の結果を表示するにはどうすればようのでしょうか

コード:

#include <stdio.h>

struct scores{
	char name[100];
	int math;
	int physics;
	int eng;
};

void printscore(struct scores st)
{
	printf("%s さんの点数\n",st.name);
	printf(" 数学: %d 点\n",st.math);
	printf(" 物理: %d 点\n",st.physics);
	printf(" 英語: %d 点\n",st.eng);
	
}

int main(void)
{
	struct scores st =
	{
		{"Itoh",85,79,90},
		{"Satoh",60,60,75},
		{"Katoh",90,55,70},
		{"Mutoh",40,50,80},
	};
	printscore(st);
	
	return 0;
}

box
記事: 2002
登録日時: 13年前

Re: {再び}C言語 構造体 関数

#2

投稿記事 by box » 7年前

こんな感じ?

コード:

#include <stdio.h>

typedef struct scores {
    char name[100];
    int math;
    int physics;
    int eng;
} SCORE;

void printscore(SCORE *st, int sz)
{
    int i;

    for (i = 0; i < sz; i++) {
        printf("%s さんの点数\n", st[i].name);
        printf(" 数学: %d 点\n",  st[i].math);
        printf(" 物理: %d 点\n",  st[i].physics);
        printf(" 英語: %d 点\n",  st[i].eng);
    }
}

int main(void)
{
    SCORE st[] = {
        { "Itoh",  85, 79, 90 },
        { "Satoh", 60, 60, 75 },
        { "Katoh", 90, 55, 70 },
        { "Mutoh", 40, 50, 80 },
    };
    
    printscore(st, sizeof(st) / sizeof(st[0]));
    return 0;
}
バグのないプログラムはない。
プログラムは思ったとおりには動かない。書いたとおりに動く。

閉鎖

“C言語何でも質問掲示板” へ戻る