Thread: external structure declaration

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    external structure declaration

    hey guys!
    Im trying to declare a struct form other file but compiler says theres an error on declaration

    file1.c

    Code:
    struct {
    	char entry[65536][128];
    } hosts;
    file2.c
    Code:
    how do I declare the global struct here?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Put the structure defnation inside a header file and #include it in both .c files

    Also the way you have it would make it a global or local variable (depending on where it is). To change it to a proper defination move the name:
    Code:
    struct hosts{
      char entry [65536][128];
    };
    Then you can assign variables to that type:
    struct hosts host1;

    If it is suppose to be a variable then you will need to do something like this:

    Header:
    Code:
    typedef struct {
      char entry [65536][128];
    }HOSTS;
    File1.c
    Code:
    HOSTS hosts;
    func1(&hosts);
    File2.c (for the function)
    Code:
    void func1(HOSTS *rec_hosts)
    Last edited by Thantos; 04-16-2004 at 04:11 PM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    this is the only way?
    i dont know to create .h files. Is it like .c files?

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    this is the only way?
    No, but the other ways are much more evil.
    i dont know to create .h files. Is it like .c files?
    Kinda of. However in the .h (header) files you normally only put function prototypes, type definations, and any macros.

    An example would be:
    Code:
    /* Check to see if this header has been included or not */
    #ifndef MY_HEADER_
    /* If it hasn't then it will define a macro to mark that its been included already.  
    If it has then it will go to the #endif */
    #define MY_HEADER_
    
    typedef struct {
      char entry [65536][128];
    }HOSTS;
    #endif
    Then you would save it as myheader.h and inside your C files you would:
    Code:
    #include "myheader.h" 
    /* Note the use of " " instead of < > */
    and then both files would have access to the type HOSTS which will allow you to freely pass data of that type around between the two files.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    18
    Thank you Thantos!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and don't forget to read up about the extern keyword.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. unresolved external symbols...linking errors in VC++
    By rammohan2b in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2009, 02:19 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. VC++ a POS
    By Welder in forum Windows Programming
    Replies: 40
    Last Post: 11-07-2007, 03:07 PM
  5. Book's code: Problematic
    By RoD in forum Game Programming
    Replies: 14
    Last Post: 01-21-2003, 09:08 AM