我正在编写一个按姓名和年级对学生列表进行排序的程序。我在尝试编译时收到以下错误:
ex11.c:在函数“compareByGrade”中: ex11.c:46:错误:在非结构或联合的情况下请求成员“等级” ex11.c:47:错误:在非结构或联合的情况下请求成员“等级” ex11.c:在函数“compareByName”中: ex11.c:56:错误:在不是结构或联合的东西中请求成员“名称” ex11.c:57:错误:在非结构或联合的情况下请求成员“名称”
这是头文件:
#define CLASS_SIZE 10
struct student {
char *name;
int idnumber;
char grade;
};
这是主文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ex11.h"
int main(void)
{
int c, i;
FILE *data;
struct student tmp, class[CLASS_SIZE];
void *nameArray[CLASS_SIZE], *gradeArray[CLASS_SIZE];
static int compareByGrade(const void *first, const void *second);
static int compareByName(const void *first, const void *second);
data = fopen("data", "r"); /* open data file */
/* Move data into struct */
for (i = 0; i < CLASS_SIZE; ++i) {
fscanf(data, "%s%d%c", tmp.name, tmp.idnumber, tmp.grade);
class[i].name = tmp.name;
class[i].idnumber = tmp.idnumber;
class[i].grade = tmp.grade;
}
fclose(data); /* close data file */
qsort(nameArray, CLASS_SIZE, sizeof(*nameArray), &compareByName);
qsort(gradeArray, CLASS_SIZE, sizeof(*gradeArray), &compareByGrade);
}
/* Function to sorty by grade */
static int compareByGrade(const void *first, const void *second)
{
const char *firstGrade = (*(const char **) first) -> grade; /* line 46 */
const char *secondGrade = (*(const char **) second) -> grade; /* line 47 */
return strcmp(firstGrade, secondGrade);
}
/* Function to sort by name */
static int compareByName(const void *first, const void *second)
{
const char *firstName = (*(const char **) first) -> name; /* line 56 */
const char *secondName = (*(const char **) second) -> name; /* line 57 */
return strcmp(firstName, secondName);
}
我究竟做错了什么?