抱歉打扰,我正在尝试将像
ANIMAL *animal[2][6]
这样的二维指针数组解析为函数,该函数将返回该二维指针数组。它得到的错误是
[Error] incompatible types in assignment of 'ANIMAL*' to 'ANIMAL* [6]'
P.S:忽略
createNode
功能,因为错误在线*animal = readFile(animal);
struct ANIMAL {
char id[10];
char species[50];
char type[10];
char strDate[50];
time_t create_date = NULL;
ANIMAL *next;
};
ANIMAL *readFile(ANIMAL *[][6]);
ANIMAL *createNode(ANIMAL *[][6], ANIMAL *, char *, int, int);
int main() {
ANIMAL *animal[2][6];
for (int j = 0; j < 2; j++) {
for (int i = 0; i < 6; i++) {
animal[j][i] = (ANIMAL *)malloc(sizeof(ANIMAL));
animal[j][i] = NULL;
}
}
*animal = readFile(animal);
}
ANIMAL *readFile(ANIMAL *animal[][6]) {
FILE *file;
ANIMAL *temp[1];
char data[180], date[20];
int i;
file = fopen("data.txt", "r");
if (file != NULL) {
while (fgets(data, sizeof(ANIMAL), file)) {
temp[0] = (ANIMAL *)malloc(sizeof(ANIMAL));
char *token = strtok(data, "|");
strcpy(temp[0]->id, token);
token = strtok(NULL, "|");
strcpy(temp[0]->species, token);
token = strtok(NULL, "|");
strcpy(temp[0]->type, token);
if (strcmp(temp[0]->type, "mamal") == 0) i = 0;
if (strcmp(temp[0]->type, "bird") == 0) i = 1;
if (strcmp(temp[0]->type, "reptile") == 0) i = 2;
if (strcmp(temp[0]->type, "amphibian") == 0) i = 3;
if (strcmp(temp[0]->type, "insect") == 0) i = 4;
if (strcmp(temp[0]->type, "unknown") == 0) i = 5;
token = strtok(NULL, "|");
strcpy(temp[0]->strDate, token);
token = strtok(NULL, "|");
strcpy(date, token);
temp[0]->create_date = atoi(date);
token = strtok(NULL, "|");
*animal = createNode(animal, temp[0], temp[0]->type, i, 2);
}
} else {
printf("No data found\n");
}
fclose(file);
return *animal;
}
ANIMAL *createNode(ANIMAL *animal[], ANIMAL *temp, char *type, int i, int condition) {
char buff[20], id[10], species[50];
time_t now = time(NULL);
ANIMAL *newNode;
newNode = (ANIMAL *)malloc(sizeof(ANIMAL));
if (condition == 2) {
newNode = temp;
newNode->next = NULL;
if (animal[i] != NULL) {
newNode->next = animal[i];
}
animal[i] = newNode;
//printf("/t%d",i);
return *animal;
}
newNode->create_date = now;
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
strcpy(newNode->strDate, buff);
insert_id:
printf("\nEnter Animal ID: ");
scanf(" %[^\n]s", &id);
if (!validation(animal, id)) {
goto insert_id;
}
strcpy(newNode->id, id);
printf("Enter Animal Species: ");
scanf(" %[^\n]s", &species);
strcpy(newNode->species, species);
strcpy(newNode->type, type);
newNode->next = NULL;
if (animal[i] != NULL) {
newNode->next = animal[i];
}
animal[i] = newNode;
return *animal;
}
在此之前,我使用一维数组并且它有效。改为二维数组后出现错误。我想知道我犯了什么错误以及如何解析和返回 2D 指针数组。
没有理由使用指向
ANIMAL
结构的指针的二维数组。第二个维度似乎对应于动物类型,但第一个维度的使用尚不清楚,可能不需要。
而且
fgets(data, sizeof(ANIMAL), file)
是不正确的,您应该传递 data
字符数组的大小,而不是 ANIMAL
结构的大小。
main
函数应该将数组元素初始化为NULL
,这可以简单地使用初始化器来完成。
readFile
不需要返回数组指针,让它返回成功/失败指示符。
这是修改后的版本:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct ANIMAL {
char id[10];
char species[50];
char type[10];
char strDate[50];
time_t create_date;
ANIMAL *next;
} ANIMAL;
bool readFile(ANIMAL animals[6]);
ANIMAL *createNode(ANIMAL *[][6], ANIMAL *, char *, int, int);
int main(void) {
ANIMAL *animal[2][6] = {
{ NULL, NULL, NULL, NULL, NULL, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL },
};
if (!readFile(animal)) {
fprintf(stderr, "cannot read animal file\n");
}
return 0;
}
const char * const animal_types[5] = { "mamal", "bird", "reptile", "amphibian", "insect" };
int get_animal_type(const char *type) {
int i;
for (int i = 0; i < 5; i++) {
if (strcmp(type, animal_types[i]) == 0) {
return i;
}
}
return 5; /* unknown */
}
bool readFile(ANIMAL animal[6]) {
char data[180], date[20];
FILE *file;
file = fopen("data.txt", "r");
if (file == NULL) {
fprintf(stderr, "Cannot open file %s: %s\n", "data.txt", strerror(errno));
return false;
}
while (fgets(data, sizeof(data), file)) {
ANIMAL *temp = (ANIMAL *)malloc(sizeof(*temp));
if (temp == NULL) {
fprintf(stderr, "memory allocation failure\n");
break;
}
char *token = strtok(data, "|");
if (!token)
break;
strcpy(temp->id, token);
token = strtok(NULL, "|");
if (!token)
break;
strcpy(temp->species, token);
token = strtok(NULL, "|");
if (!token)
break;
strcpy(temp->type, token);
int i = get_animal_type(temp->type);
token = strtok(NULL, "|");
if (!token)
break;
strcpy(temp->strDate, token);
token = strtok(NULL, "|");
if (!token)
break;
strcpy(date, token);
temp->create_date = atoi(date);
createNode(animal, temp, temp->type, i, 2);
}
fclose(file);
return true;
}
ANIMAL *createNode(ANIMAL *animal[], ANIMAL *temp, char *type, int i, int condition) {
char buff[20], id[10], species[50], type[10];
if (condition == 2) {
temp->next = animal[i];
animal[i] = temp;
return temp;
}
time_t now = time(NULL);
ANIMAL *newNode = (ANIMAL *)malloc(sizeof(ANIMAL));
newNode->create_date = now;
strftime(newNode->strDate, sizeof newNode->strDate, "%Y-%m-%d %H:%M:%S", localtime(&now));
for (;;) {
printf("\nEnter Animal ID: ");
scanf(" %9[^\n]", id);
if (validation(animal, id))
break;
}
strcpy(newNode->id, id);
printf("Enter Animal Species: ");
scanf(" %49[^\n]", species);
strcpy(newNode->species, species);
printf("Enter Animal Type: ");
scanf(" %9[^\n]", type);
strcpy(newNode->type, type);
i = get_animal_type(type);
newNode->next = animal[i];
animal[i] = newNode;
return newNode;
}