0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【C言語】構造体はじめました

0
Posted at

はじめに

こんにちは、Juna1013です。
ポインタに引き続き、今週2本目の投稿です。

今回はC言語にて構造体の勉強をはじめた(授業でやらざるを得なくなった)ため、発生したエラーとその原因・対処についてまとめていこうと思います。

構造体って何だっけ?

構造体とは、新しい「型」を作成する仕組みです。互いに関連する複数のデータを「メンバ」と呼ばれる各データ要素で構成します。
それぞれのメンバは異なるデータ型を持つことができます。配列とは異なり、異なるデータ型(整数、文字列、浮動小数点数)を扱えるのが特徴です。

何やらかしたの?

アルゴリズムの授業で以下のような課題が出されました。

Q. 下のプログラムには、冒険者の Adventurer 構造体が宣言されています。引数として構造体を受け取って、ステータスを表示する print_status 関数を完成させてください。

// RPGの冒険者のステータスを出力しよう
#include <stdio.h>

typedef struct {
    char job[30];
    int mp;
} Adventurer;


void print_status() // 適切に引数を指定する
{
    printf("Job: %s, MP: %d\n", ); // 出力する値を設定する
}

int main(void)
{
    Adventurer adventurer = {"冒険者", 120};
    Adventurer wizard = {"ウィザード", 549};
    Adventurer crusader = {"クルセイダー", 50};
    Adventurer priest = {"プリースト", 480};

    print_status(adventurer);
    print_status(wizard);
    print_status(crusader);
    print_status(priest);
}

私が最初に実装したコードは以下の通りです。

#include <stdio.h> 

typedef struct { 
    char job[30]; 
    int mp; 
} Adventuer; 

void print_status(job) { 
    printf("Job: %s, MP: %d\n"); 
} 

int main(void) { 
    Adventuer adventuer = {"冒険者", 120}; 
    Adventuer wizard = {"ウィザード", 549}; 
    Adventuer crusader = {"クルセイダー", 50}; 
    Adventuer priest = {"プリースト", 480}; 
    
    print_status(adventuer); 
    print_status(wizard); 
    print_status(crusader); 
    print_status(priest); 
    
    return 0; 
}

コンパイルしようとすると、以下のようなエラーメッセージが出力されました。

kadai07.c:8:19: error: parameter 'job' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int] 8 | void print_status(job) { | ^ 
kadai07.c:9:19: warning: more '%' conversions than data arguments [-Wformat-insufficient-args] 9 | printf("Job: %s, MP: %d\n"); | ~^ 
kadai07.c:8:6: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C23 [-Wdeprecated-non-prototype] 8 | void print_status(job) { | ^ 
kadai07.c:18:18: error: passing 'Adventuer' to parameter of incompatible type 'int' 18 | print_status(adventuer); | ^~~~~~~~~ 
kadai07.c:19:18: error: passing 'Adventuer' to parameter of incompatible type 'int' 19 | print_status(wizard); | ^~~~~~ 
kadai07.c:20:18: error: passing 'Adventuer' to parameter of incompatible type 'int' 20 | print_status(crusader); | ^~~~~~~~ 
kadai07.c:21:18: error: passing 'Adventuer' to parameter of incompatible type 'int' 21 | print_status(priest); | ^~~~~~ 2 
warnings and 5 errors generated.

エラーメッセージによると8行目でエラーを吐いているようでした。
チャッピーに対処法を確認したところ、

  • jobの型が指定されていない
  • printf()の引数が足りない

との指摘をされました。

1. jobの型が指定されていない

// 修正前
void print_status(job) {

まず、8行目のエラーです。
ここでjobの型をしていませんでした。
C89までは暗黙のintが許されていましたが、C99以降は禁止らしいです。

// 修正後
void print_status(Adventuer job) {

構造体Adventuer型の変数jobを引数に取る関数です。

2. printf()の引数が足りない

// 修正前
printf("Job: %s, MP: %d\n");

フォーマット指定子%sと%dがあるのに、対応する変数がありませんでした。
(というか、その変数を追加する課題なのに忘れてた...)

// 修正後
printf("Job: %s, MP: %d\n", job.job, job.mp);

おわりに

競プロだと構造体を使う機会がほぼない(私のレベルだと)ので、これから頑張って勉強していきます!

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?