简体   繁体   中英

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

Every file which requires the declaration of your types (ie, 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) {
    /* ... */
}

Here is some advice.

  • Your .h file is not defining struct objects. It's just defining the type. It's fine the way it is. Everyone who touches any struct of those types should include this file.

  • It's very rare to need to pass a struct by value as you are doing in the call to ga_init . You will essentially always want to call by reference, like you did with io_init .

  • Yes, you can return a struct, but again, it would almost always be better to return a reference to a struct.

  • You can certainly share globally defined structs and you don't need extern unless your linker is something awful. But sharing a reference to a struct allocated in main() amounts to roughly the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM