gpt4 book ai didi

C - 无法将结构指针传递给函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:04 25 4
gpt4 key购买 nike

当我编译这个时,我得到很多错误,说我的 struct 的每个部分都是无效的,按照以下行:

sort.c:16: 请求成员“last”不是结构或 union

对于我使用 strcpy 的情况,错误如下:

sort.c:18: 警告:从不兼容的指针类型传递 'strcpy' 的 arg 2

所以我一定是误用了指针...但我不确定为什么。

我在 DBrecord.h 中定义了 struct:

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;
}

最佳答案

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

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

关于C - 无法将结构指针传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10230167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com