This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Extern STRUCT..

Hi, I have a big problem. I have a multyple file included into the project, and in the main.c i've declared a struct like this:

struct{
unsigned char x;
unsigned char y;
unsigned char z;
}prova;


My problem is, how can i use this struct on other file included? ("extern struct.... not work").
The compiler return error L6200E.
Help me,Thanks!.

  • Easy - check C programming books about using data types. And how to use header files (#include) to let the different C files know about your data types and global variables.

  • Define the struct like this in some header file:

    struct tagStructName{
    ...
    };
    
    extern struct tagStructName prova;
    

    Then you can use prova in any c module, which includes this header file.

    In exactly one c module, you will have to define it:

    struct tagStructName prova;
    

    or:

    struct tagStructName prova= {
      ... initialisation data ...
    };
    

    If you want, you can change the header file definition also to the following:

    typedef struct tagStructName{
     ...
    }STRUCTNAME;
    

    Then anyway, where you normally needed to write "struct tagStructName", you can now write more shortly just "STRUCTNAME".

    Good luck with your structs :)!

  • typedef struct tagStructName{
     ...
    }STRUCTNAME;
    

    Then anyway, where you normally needed to write "struct tagStructName", you can now write more shortly just "STRUCTNAME".

    Be aware, though, that it's generally considered a bad idea in C programs to name anything that's not a preprocessor or possibly an enum tag in ALL_CAPITALS.

  • You can principally skip the "tagStructName". But to my experience then sometimes debugger / watch windows get problems - so better give a unique name to every struct you define.