0

当我编译这个时,我得到很多错误,说我的每个部分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;
}
4

2 回答 2

3

因为recordis 指向指向 a 的指针DBrecordrecord->last[j]所以没有任何意义。(指向指针的指针)处的对象record没有名为last;的字段。它根本没有字段,它只是一个指针。

您可能的意思是record[j]->last,获取last第 j:th DBrecord 的成员。等等。

于 2012-04-19T14:23:27.743 回答
2

record->last[j]。这相当于(*record).last[j]。但*record不是一个struct。它是类型DBrecord*。换句话说,您需要取消引用您的指针两次,但您只取消引用了一次。

于 2012-04-19T14:23:49.187 回答