0

I'm try to use my struct peer_struct.h in multiple files. It is declared in main.c and I pass the struct trough reference.

The compiler gives me warings for the functions and I really don't understand why.

compiler Warnings:

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);
Leon
  • 1
  • you should not declare an instance of anything in a header file. Rather the header file should only contain the definition and some .c file (like main) should declare in instance of the struct – user3629249 Nov 30 '18 at 01:49

1 Answers1

1

The very first warning tells it all. When you include first_use.h in main.c, the only thing which gets included is the prototype for the function

void first_use(struct Peer_Information *peer);

The type Peer_Information is not yet defined, so compiler generates a type with the same name which is local to this function. Than nothing works as it should, because you end up calling the function with the different (from compiler's standpoint) type which just happen to have the same name.

To fix it, you should make it a habit to include all header files required for the header files. In your case, include definition of the type as well and make your first_use.h to be like following:

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

In addition to that, your include file misses so-called include guard. You can get more details about those here: What exactly do C include guards do?

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • That was my first try but then it says peer_struct.h "error: redefinition of ‘struct Peer_Information’" – Leon Nov 29 '18 at 22:41
  • @Leon because your header file doesn't have include guard. I missed this part, I will update my answer. – SergeyA Nov 29 '18 at 22:42
  • thank you so much, would have never find out that on my own – Leon Nov 29 '18 at 22:55