当我编译这个时,我得到很多错误,说我的每个部分struct
都是无效的,大致如下:
sort.c:16: request for member 'last' in something not a structure or union
对于我使用 strcpy 的情况,错误显示为:
sort.c:18: warning: passing arg 2 of 'strcpy' from incompatible pointer type
所以我一定是在滥用指针……但我不知道为什么。
我有struct
定义DBrecord.h
:
typedef struct{
int DBrecordID; //ID for each entry, range 0-319
char *last; //student last name
char *first; //student first name
char studentID[8]; //student ID
int age; //student age
class year; //year in school
float gpa; //GPA
int expGradYear; //expected graduation year
}DBrecord;
#include <stdio.h>
#include <string.h>
#include "DBrecord.h"
void bubbleSort(DBrecord **record, int numEntries, int sortChoice) {
int i, j;
char temp[100];
for(i=0; i<numEntries; i++)
for(j = 0; j < numEntries-1; j++)
switch(sortChoice){
//sort by last name
case 1 : if(strcmp(record->last[j], record->last[j+1]) > 0){
//swap the two elements
strcpy(temp, record[j]);
strcpy(record[j], record[j+1]);
strcpy(record[j+1], temp);
}
//sort by first name
case 2 : if(strcmp(record->first[j], record->first[j+1]) > 0){
//swap the two elements
strcpy(temp, record[j]);
strcpy(record[j], record[j+1]);
strcpy(record[j+1], temp);
}
//sort by student ID
case 3 : if(atoi(record->studentID[j]) > atoi(record->studentID[j+1])){
//swap the two elements
temp = record[j];
record[j] = record[j+1];
record[j+1] = temp;
}
//sort by age
case 4 : if(atoi(record->age[j]) > atoi(record->age[j+1])){
//swap the two elements
temp = record[j];
record[j] = record[j+1];
record[j+1] = temp;
}
//sort by class
case 5 : if(record->class[j] > record->class[j+1]){
//swap the two elements
temp = record[j];
record[j] = record[j+1];
record[j+1] = temp;
}
//sort by gpa
case 6 : if(atoi(record->gpa[j]) > atoi(record->gpa[j+1])){
//swap the two elements
temp = record[j];
record[j] = record[j+1];
record[j+1] = temp;
}
//sort by expected graduation year
case 7 : if(atoi(record->expGradYear[j]) > atoi(record->expGradYear[j+1])){
//swap the two elements
temp = record[j];
record[j] = record[j+1];
record[j+1] = temp;
}
default : break;
}