Thread: Using an instance of a struct in one .c file in another .c file

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    Using an instance of a struct in one .c file in another .c file

    main.c
    Code:
    #include "myHeaders.h"
    
     int main(void)
    {
        printf("Hello World!", struct1.myInt);
    }
    myHeaders.h
    Code:
    extern struct myStruct;
    
    struct myStruct{
        int myInt;
    };
    myFuncs.c
    Code:
    #include "myHeaders.h"
    
    struct myStruct struct1;
    Compiler complains, in main.c, that "struct1 is undeclared (first use in this function)."

    This makes no sense. Ofcourse its declared. Its declared in the header file. The header file is in turn included in main.

    Whats wrong?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    "struct myStruct" is a type; you can't extern a type.

    Perhaps you were thinking more along the lines of this:

    Code:
    /* -- myheaders.h --------------------------*/
    #ifndef MYHEADERS_H
    #define MYHEADERS_H
    
    struct myStruct{
        int myInt;
    };
    
    #endif  /* MYHEADERS_H */
    
    
    /* -- myheaders.c --------------------------*/
    #include "myheaders.h"
    
    struct myStruct struct1 = { 4 };
    
    
    /* -- main.c -------------------------------*/
    #include <stdio.h>
    #include "myheaders.h"
    
    extern struct myStruct struct1;
    
    int main(void)
    {
        printf("%d\n", struct1.myInt);
    
        return 0;
    }
    Note how the struct is declared in the .c file, not the header.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Thanks - so you're saying to use it I have to extern the instance and not the type to make the instance visible throughout. That makes sense...

    But in your main.c I would take the
    extern struct myStruct struct1;
    out and into the header file instead. My project works easier like that. Or is that not good practice? Its a declaration and declarations should stay in the header. Thoughts?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    It's actually a definition (as well as a declaration), so it should be in a source (not header) file. So yes, it is not good practice to have in your header file.

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Okay. I think what I meant was if it was
    extern struct myStruct;
    then it would be a declaration and would stay in the header. But because its instantiated
    extern struct myStruct struct1;
    then it goes to the .c file. Is my interpretation correct?

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    As was already mentioned, "extern struct myStruct" is meaningless since you can't "extern" a type.

    And "extern struct myStruct struct1" doesn't "instantiate" anything. It just makes struct1 available for use in that source file. It could actually go in the header file. Matticus was saying that the instantiation (definition)
    Code:
    struct myStruct struct1 = { 4 };
    needs to go in a source file.
    It could actually go in the source file with main instead of it's own source file.
    It just has to be in one and only one source file in the project.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-03-2013, 10:03 PM
  2. Replies: 10
    Last Post: 10-14-2011, 01:31 PM
  3. creating class instance from a file
    By Callith in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2004, 09:03 PM
  4. getting file size from FILE* struct
    By duck-billed platypus in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2002, 02:03 AM
  5. Detecting the current instance of exe file
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 09-03-2001, 08:22 PM

Tags for this Thread