您好!
我需要在创建学生列表系统时使用malloc函数...为了提高效率,我们的教授要求我们在结构体上使用它,因此我创建了以下结构体:
struct student {
char studentID[6];
char name[31];
char course [6];
};
struct student *array[30];
每当我添加一条记录时,就会使用malloc...
array[recordCtr]=(struct student*)malloc(sizeof(struct student));
recordCtr++;
然后我像这样释放它。
for(i = 0; i < recordCtr; i++){
free(array[i]);
}
我是否正确使用了malloc?如果我像下面这样释放它,而不是使用循环,会有什么影响。
free(array);
非常感谢。我们非常重视您的意见。