c - 传递和返回结构体 C

标签 c function struct

所以我遇到了这个问题,我只在主文件中不断收到错误代码:(1) 当我将结构保留在头文件中时,该结构已经被定义,并且 (2) 我使用了不兼容的指针类型。

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

然后这是我的函数定义:

void writeToFile(struct contactId *contInfo, int numContacts);
struct contactId *inputContactInfo();
void readFile(int numContacts);

这是结构头文件:

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 contactId **contactInfo,为每个元素分配内存,然后您可以正确地将指针分配给contactInfo[i]。

关于c - 传递和返回结构体 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43145116/

相关文章:

ruby-on-rails - 在 Ubuntu 服务器上通过 ruby​​ 脚本启动 C 可执行文件

c++ - gcc 链接器获取未使用对象的列表

php - 我该如何修复 Heroku imagecreatefromjpeg()

php - 来自不同表的上一年记录

c - 如何为 typedef 结构正确分配内存

c - C 中的前/中/后缀遍历(包括数组)

c - 在 C 中从父进程向子进程发送信号

swift - 如何在不通过选择器 Swift 的情况下将参数传递给方法?

C - 函数内结构数组的多个 malloc

iphone - Objective-C 和 C++ 中结构的大小