C - 将结构指针传递给函数时遇到麻烦

时间:2012-04-19 14:21:26

标签: c struct function-pointers

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

2 个答案:

答案 0 :(得分:3)

由于record是指向DBrecord指针的指针,record->last[j]不会生成任何指针 感。 record处的对象(指向指针的指针)没有名为last的字段;它根本没有字段,它只是一个指针。

你可能意味着record[j]->last,以获得j:th DBrecord的last成员。等等。

答案 1 :(得分:2)

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