Search code examples
c

Using structs in multiple files


I am trying to call a function in main.c from io.h that reads data from a file, stores that data into multiple structs, then somehow lets me pass the different structs as arguments in later functions in main. Those later functions will be defined in other files, such as alg.h.

How do I go about doing this? Do I use extern and make the structs global and put them in a separate file? Is it possible to have a function from alg.h have a return type of one of the structs? Does it depend on the order of my includes?

The code pasted below complies and works, but any attempt to move either of the structs causes the program to not compile.

Also, is it possible to have, for example, a struct declared in alg.h, then functions that have that struct as a parameter declared later in alg.h. Then in main.c, you initialize and pass the struct into a function declared in io.h, give the struct some values, have it returned to main.c, then pass that into the function declared in alg.h? I know that sounds like a class, but I need a C solution and I only need one instance of the struct floating around.

Thanks.

io.h

struct s1 {
    int num1;
    double num2;
};

struct s2 {
    int num3;
    double num4;
};

void io_init(struct s1*, struct s2*);

io.c

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

#include "io.h"

void io_init(struct s1* s1i, struct s2* s2i)
{
    s1i->num1 = 5;
    s1i->num2 = 2.4;

    FILE *fp;
    char line[80];

    fp = fopen("input.txt","rt");

    fgets(line, 80, fp);
    sscanf(line,"%i",&s2i->num3);
    fgets(line, 80, fp);
    sscanf(line,"%i",&s2i->num4);

    fclose(fp);
}

alg.h

void ga_init(struct s1);

alg.c

#include <stdio.h>

#include "io.h"
#include "ga.h"

void ga_init(struct s1 s1i)
{
    printf("%i", s1i.val1);
}

main.c:

#include <stdio.h>

#include "io.h"
#include "ga.h"

int main() {
    struct s1 s1i;
    struct s2 s2i;

    io_init(&s1i, &s2i);
    ga_init(s1i);

    return 0;
}

Solution

  • Every file which requires the declaration of your types (i.e., wants to use them) must include your header file (ok, so forward declarations and pointers will work, but they can't be dereferenced without the definition and that's not really applicable here anyway.)

    So, to elaborate, if file X needs to use struct Y then it needs to include the header file which contains its declaration, that's it.

    /* X.c */
    #include "Y.h"  /* <-- that's it! */
    
    void foo(Y *obj) {
        /* ... */
    }