Student_name の値を出力しようとすると、null しか返されない理由を理解してもらえますか? 学生の名前、ID、および 2 つのテストを格納するために、C で基本的なハッシュテーブルを実装しています。他のすべては正しく保存されています。何を試しても、student_name を保存できません。ハッシュテーブル自体と、テーブル内に配置する要素を記録する 2 つの構造体があります。文字列が 18 文字を超えることはありません。
int main(){
char op[1];
int stu_id[1];
int exam1[1];
int exam2[1];
char * student_name = (char*)malloc(18*sizeof(char));
struct hashtable * dictionary = malloc(sizeof(struct hashtable));
dictionary->size = 13;
dictionary->table = malloc(13*sizeof(struct record *));
if(dictionary==NULL||dictionary->table==NULL){
printf("Unable to allocate memory for the dictionary.\n");
return;
}
int i;
int s = 13;
while(i<s){
dictionary->table[i]=NULL;
i++;
}
while(scanf("%s %d %d %d %s", op, stu_id, exam1, exam2, student_name) !=EOF){
if(*op=='i'){
printf("Intializing %s\n", *student_name);
add_item(dictionary, stu_id[0], exam1[0], exam2[0], student_name);
}
free(dictionary);
free(student_name);
return 0;
}