传递和返回结构C
因此,我遇到了这样的问题:我只在main中获取错误代码,1当我将结构保存在头文件中时,结构已经被定义,2我使用的是不兼容的指针类型传递和返回结构C,c,function,struct,C,Function,Struct,因此,我遇到了这样的问题:我只在main中获取错误代码,1当我将结构保存在头文件中时,结构已经被定义,2我使用的是不兼容的指针类型 #include <stdlib.h> #include <stdio.h> #include <string.h> #include "functDefs.h" #include "writeToFile.c" #include "readFile.c" #include "inputContactInfo.c" #includ
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "functDefs.h"
#include "writeToFile.c"
#include "readFile.c"
#include "inputContactInfo.c"
#include "contactInfoStruct.h"
int main(void) {
int i = 0;
char *ynAns;
struct contactId *contactInfo;
contactInfo = malloc(sizeof(struct contactId));
do {
if(ynAns != NULL) {
free(ynAns);
}
ynAns = malloc(sizeof(char) * 5);
printf("\nDo you wish to enter a new contact (Yes or No)?: ");
fgets(ynAns, 5, stdin);
ynAns[(strlen(ynAns) - 1)] = '\0';
if (strcmp(ynAns, "Yes") == 0) {
printf("\n");
contactInfo = realloc(contactInfo, sizeof(struct contactId) * (i + 1));
contactInfo[i] = inputContactInfo();
i++;
}
} while(strcmp(ynAns, "No") != 0);
writeToFile(contactInfo, i);
readFile(i);
free(contactInfo);
return 0;
}
这是结构头文件:
struct contactId {
char firstName[20];
char lastName[20];
char companyName[50];
char phoneNumber[15];
char email[50];
};
我会遇到如下错误:
IOlist.c: In function ‘main’:
IOlist.c:28:40: error: incompatible types when assigning to type ‘struct contactId’ from type ‘struct contactId *’
contactInfo[i] = inputContactInfo();
^
IOlist.c:34:21: warning: passing argument 1 of ‘writeToFile’ from incompatible pointer type
writeToFile(contactInfo, i);
^
In file included from IOlist.c:5:0:
writeToFile.c:7:6: note: expected ‘struct contactId *’ but argument is of type ‘struct contactId *’
void writeToFile(struct contactId *contInfo, int numContacts) {
^
还有这些错误:
In file included from IOlist.c:5:0:
writeToFile.c:7:6: error: conflicting types for ‘writeToFile’
void writeToFile(struct contactId *contInfo, int numContacts) {
^
In file included from IOlist.c:4:0:
functDefs.h:1:6: note: previous declaration of ‘writeToFile’ was here
void writeToFile(struct contactId *contInfo, int numContacts);
^
In file included from readFile.c:4:0,
from IOlist.c:6:
contactStruct.h:1:8: error: redefinition of ‘struct contact’
struct contact {
^
In file included from writeToFile.c:4:0,
from IOlist.c:5:
contactStruct.h:1:8: note: originally defined here
struct contact {
^
函数inputContactInfo返回指向struct的指针。但它试图返回n指针的地方是一个结构。您需要声明struct contactId**contactInfo,为每个元素分配内存,然后才能正确地将指针分配给contactInfo[i]。这些“错误”的部分原因是,在声明writeToFile函数之前,您没有确保编译器知道struct contactId存在。原型中的类型仅在原型中已知。只需添加line struct contactId;在函数头的顶部,问题可能会消失,或者会有一组不同的问题需要处理。
In file included from IOlist.c:5:0:
writeToFile.c:7:6: error: conflicting types for ‘writeToFile’
void writeToFile(struct contactId *contInfo, int numContacts) {
^
In file included from IOlist.c:4:0:
functDefs.h:1:6: note: previous declaration of ‘writeToFile’ was here
void writeToFile(struct contactId *contInfo, int numContacts);
^
In file included from readFile.c:4:0,
from IOlist.c:6:
contactStruct.h:1:8: error: redefinition of ‘struct contact’
struct contact {
^
In file included from writeToFile.c:4:0,
from IOlist.c:5:
contactStruct.h:1:8: note: originally defined here
struct contact {
^