Smiley7
Smiley7

Reputation: 235

Including .h files for functions

I am passing a struct pointer to a function in another .c file. Do I need to include this header file in both .c files?

//test.h
typedef struct {
   int number;

} STATS;

//test.c
#include "test.h"
void function(){

   func2(s);
}

//stats.c
//do I need : include "test.h"
void func2(STATS * ptr){
    ptr->number = 10;
}

Upvotes: 0

Views: 39

Answers (2)

nuke_infer
nuke_infer

Reputation: 63

Yes you have to instantiate the struct class because u declared the number variable in .h class

Upvotes: 0

Barmar
Barmar

Reputation: 780688

Yes, stats.c needs the structure definition in order to know where the num member is in the STATS structure.

If it were simply passing the pointer along to some other function, it wouldn't need the structure definition; you would just need a forward declaration of the structure type name; all structure pointers are required to be compatible this way, and that allows the pointer to be treated as an opaque handle. But since func2 accesses a member, it's not opaque.

Upvotes: 1

Related Questions