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言語の構造体の使い方を考えてみた

Posted at

C言語学習1か月程度で、構造体理解をするため作成したコードです。
変数の数だけscanfで構造体を登録できるため、何かに使えそうな気がしますがひらめきません。
始めたばかりで構造体に悩まれている方の参考になれば。

struct.c
#include <stdio.h>
#include <string.h>

struct schedule {//構造体の定義(一番上)
    char name[100];//文字列は配列にすること
	int year;        /* 年 */
    int month;       /* 月 */
    int day;         /* 日 */
};

//配列の参照はvoid
//配列の値渡しはstruct+構造体名+*(ポインタ)定義名
void arr_change(struct schedule *schedules)
//構造体をプリント
{
	for (int i = 0; i < 2; i = i + 1)
	    {
	        printf("関数上は\n%s/%04d/%02d/%02d\n"
	        		,schedules[i].name
					,schedules[i].year
					,schedules[i].month
					,schedules[i].day);
	        printf("\n");
	    }
}

int main() {
	//構造体の定義はstruct+構造体名+任意の文字(配列なら[n])
    int x =2;//今回登録する数
    int i;
	struct schedule schedules[x];

    for(i=0;i<x;i++){
	   printf("名前を入力:"); scanf("%s",schedules[i].name );
	   printf("年を入力:"); scanf("%d",&schedules[i].year );
	   printf("月を入力:"); scanf("%d",&schedules[i].month );
	   printf("日を入力:"); scanf("%d",&schedules[i].day );
	   printf("\n");
    }
   arr_change(&schedules);
   getchar();
   getchar();
   return 0;
}
0
0
2

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?