hey guys!
Im trying to declare a struct form other file but compiler says theres an error on declaration
file1.c
file2.cCode:struct { char entry[65536][128]; } hosts;
Code:how do I declare the global struct here?
hey guys!
Im trying to declare a struct form other file but compiler says theres an error on declaration
file1.c
file2.cCode:struct { char entry[65536][128]; } hosts;
Code:how do I declare the global struct here?
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:
Then you can assign variables to that type:Code:struct hosts{ char entry [65536][128]; };
struct hosts host1;
If it is suppose to be a variable then you will need to do something like this:
Header:
File1.cCode:typedef struct { char entry [65536][128]; }HOSTS;
File2.c (for the function)Code:HOSTS hosts; func1(&hosts);
Code:void func1(HOSTS *rec_hosts)
Last edited by Thantos; 04-16-2004 at 04:11 PM.
this is the only way?
i dont know to create .h files. Is it like .c files?
No, but the other ways are much more evil.this is the only way?
Kinda of. However in the .h (header) files you normally only put function prototypes, type definations, and any macros.i dont know to create .h files. Is it like .c files?
An example would be:
Then you would save it as myheader.h and inside your C files you would: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
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.Code:#include "myheader.h" /* Note the use of " " instead of < > */
Thank you Thantos!
... 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]