Skip to content

Instantly share code, notes, and snippets.

Created May 10, 2015 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8534a3aea078e6fc5e75 to your computer and use it in GitHub Desktop.
Save anonymous/8534a3aea078e6fc5e75 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
char name[256];
int age;
char sex[256];
}data;
void read_profile(data *profile);
void show(data *profile);
int main(void)
{
data *profile;
int i = 0,count = 0,size = 2;
/*初期領域の確保*/
profile = (data*)malloc(sizeof(data) * size);
if(profile == NULL)exit(0);
/*入力関数呼び出し*/
while(1){
printf("%d人目の情報を入力して下さい。\n年齢に「-1」で終了。\n",i+1);
read_profile(&profile[i]);
if(profile[i].age == -1){
printf("入力を終了します\n");
break;
}
i++;
if(i+1 > size){
size += 2;
printf("領域確保!!!\n");
profile = (data*)realloc(profile,sizeof(data) * size);
}/*追加領域の確保*/
}
count = i - 1; /*情報の数をcount変数に代入*/
i = 0;
/*出力関数呼び出し*/
while(i <= count){
if(count == -1)break;
printf("%d人目の情報を出力します。\n",i+1);
show(&profile[i]);
i++;
}
printf("処理を完了しました。\n");
free(profile);
return 0;
}
void read_profile(data *profile){
printf("名前を入力して下さい。 : ");
scanf("%s",profile->name);
printf("年齢を入力して下さい。 : ");
scanf("%d",&profile->age);
printf("性別を入力して下さい。 : ");
scanf("%s",profile->sex);
}
void show(data *profile){
printf("名前: %s\n",profile->name);
printf("年齢: %d\n",profile->age);
printf("性別: %s\n",profile->sex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment