在文件之间的函数中传递结构:“预期的'struct Peer_Information *',但参数的类型为'struct Peer_Information *”

时间:2018-11-29 22:22:20

标签: c struct

我尝试在多个文件中使用struct peer_struct.h。它在main.c中声明,我通过了结构槽引用。

编译器警告我这些功能,我真的不明白为什么。

编译器警告:

In file included from first_use.c:1:0:
second_use.h:1:51: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
 void second_use(char *message, int number, struct Peer_Information *peer);
                                                   ^~~~~~~~~~~~~~~~
first_use.c: In function ‘first_use’:
first_use.c:6:23: warning: passing argument 3 of ‘second_use’ from incompatible pointer type [-Wincompatible-pointer-types]
     second_use(me, 5, peer);
                       ^~~~
In file included from first_use.c:1:0:
second_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
 void second_use(char *message, int number, struct Peer_Information *peer);
      ^~~~~~~~~~
In file included from main.c:4:0:
first_use.h:1:23: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
 void first_use(struct Peer_Information *peer);
                       ^~~~~~~~~~~~~~~~
main.c: In function ‘main’:
main.c:28:15: warning: passing argument 1 of ‘first_use’ from incompatible pointer type [-Wincompatible-pointer-types]
     first_use(&peer);
               ^
In file included from main.c:4:0:
first_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
 void first_use(struct Peer_Information *peer);

peer_struct.h

struct Peer_Information {
    char ownIP[16];
    char ownPort[6];
    unsigned int ownID;
    char successor_IP[16];
    char successor_Port[6];
    unsigned int successor_ID;
    char predecessor_IP[16];
    char predecessor_Port[6];
    unsigned int predecessor_ID;
} Peer_Information;

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "first_use.h"
#include "peer_struct.h"

void init_peer(char *argv[], struct Peer_Information *peer){
//Fills struct up, not import for question
}

int main(int argc, char *argv[]) {
    if (argc != 10) {
        printf("Nicht genügend Parameter \n");
        return -1;
    }
    struct Peer_Information peer;
    init_peer(argv, &peer);
    first_use(&peer);
    return 0;
}

first_use.c

#include "second_use.h"
#include "peer_struct.h"

void first_use(struct Peer_Information *peer) {
    char me[] = "Hello";
    second_use(me, 5, peer);
}

first_use.h

void first_use(struct Peer_Information *peer);

second_use.c

#include <stdio.h>
#include <stdlib.h>
#include "peer_struct.h"

void second_use(char *message, int number, struct Peer_Information *peer) {
    printf("%d", peer->ownID);
}

second_use.h

void second_use(char *message, int number, struct Peer_Information *peer);

1 个答案:

答案 0 :(得分:1)

第一个警告说明了一切。当您在first_use.h中包含main.c时,唯一包含的是函数的原型

void first_use(struct Peer_Information *peer);

类型Peer_Information尚未定义,因此编译器生成的名称与该函数本地的类型相同。没有什么比它应该的要有用的了,因为您最终使用恰好具有相同名称的不同类型(从编译器的角度来看)调用该函数。

要修复此问题,应养成包括头文件所需的所有头文件的习惯。在您的情况下,还包括类型的定义,并使first_use.h如下所示:

#include "peer_struct.h"
void first_use(struct Peer_Information *peer);

除此之外,您的包含文件缺少所谓的包含保护。您可以在此处获取有关这些内容的更多详细信息:What exactly do C include guards do?