私のコードでmallocを適用しようとしていますが、まだ問題があります。「構造体または共用体ではないメンバーのIDを要求しています」というエラーがあります。問題malloc
アレイの代わりにmallocを使用し、各インデックスに構造体を格納することです。配列[i] - > idを試しましたが、テキストファイルに保存されたガベージ文字の束です。私は私とループを使用していない、だってユーザーのみ入力が一度...これは私のコードであることもインクリメント:
#include<stdio.h>
#include<stdlib.h>
struct studentinfo{
char id[8];
char name[30];
char course[5];
}s1;
main(){
int i=0;
FILE *stream = NULL;
stream = fopen("studentinfo.txt", "a+");
struct studentinfo *array[50];
array[i] = (struct studentinfo*) malloc(sizeof(struct studentinfo));
printf("Enter Student ID: ");
scanf("%s", array[i].id);
fflush(stdin);
printf("Enter Student Name: ");
gets(array[i].name);
fflush(stdin);
printf("Enter Student Course: ");
scanf("%s", array[i].course);
fprintf(stream, "\n%s,\t%s,\t%s", array[i].id, array[i].name, array[i].course);
i++;
fclose(stream);
free(array);
getch();
}
は、あなたが私を助けることを願って...事前に感謝を:)
'fflush(stdin)'は未定義の動作を呼び出します。 –
そしてgets()関数を変更してください "QUOTE"これはバッファオーバーフローを作成する悪魔のツールです "... http://stackoverflow.com/questions/4346598/gets-function-in-c – newbie
あなたはStudentInfoの配列、またはStudentInfoへのポインタの配列を作成したかったのですか?あなたは後者をしたので、前者を望んでいたようです。 –