Search code examples
cstructheaderextern

How to use a struct from another file?


I am trying to use an *.h named structures in other files like clube.c which will create an array from the struct defined as Clubes.

structures.h:

extern typedef struct Clubes{
 char *codClube;
 char *nome;
 char *estadio;
};

no matter what I do it just doesn't appear in my clubes.c

Note: already did the include "structures.h"

Once again thanks in advance, and if theres any info that I can give to help you help me just ask.


Solution

  • Keyword typedef just lets you define type, for example, typedef int whole_number this will create type whole_number and now you can use it as you would use int, whole_number x = 5; Same thing with structures. People use typedef on structures so they don't need to write struct Clubes:

    typedef struct Clubes{
     char *codClube;
     char *nome;
     char *estadio;
    } clubes;
    

    And now you don't have to use struct Clubes x;, you can use clubes x; which is shorter and easier to write.

    Extern keyword is giving you global linkage, and in this case it doesn't do anything.

    Your question is a little bit confusing. If you want to create this structure, and then use it in other files you need to create header file:

    #ifndef __CLUBES_H_
    #define __CLUBES_H_ 1
    
    struct Clubes{
     char *codClube;
     char *nome;
     char *estadio;
    } clubes;
    
    #endif
    

    Save this in one header file, for example clubes.h and then in some other c code where you want to use this structure you just include header with #include "clubes.h"