Problems with "external struct"
-
Hello all, I know this is not a C list but maybe you can give me some help. Here is my problem : I have this structure defined in file aaa.h typedef struct _my_struct{ char str1[100]; char str2[100]; } my_struct; in the file bbb.c I have a function like this : void my_function(int x, my_struct *p_my_struct) { ... /* doing something*/ } in the coresponding header bbb.h I have the declaration of thefunction : void my_function(int x, my_struct *p_my_struct); how can I declare my_struct in bbb.h as being external ? I've used "external struct my_struct" but it's not compiling. Later I've tried out all the possible combinations but still not compiling. I don't want to include aaa.h in bbb.h TIA, Thomas
-
Hello all, I know this is not a C list but maybe you can give me some help. Here is my problem : I have this structure defined in file aaa.h typedef struct _my_struct{ char str1[100]; char str2[100]; } my_struct; in the file bbb.c I have a function like this : void my_function(int x, my_struct *p_my_struct) { ... /* doing something*/ } in the coresponding header bbb.h I have the declaration of thefunction : void my_function(int x, my_struct *p_my_struct); how can I declare my_struct in bbb.h as being external ? I've used "external struct my_struct" but it's not compiling. Later I've tried out all the possible combinations but still not compiling. I don't want to include aaa.h in bbb.h TIA, Thomas
Simply write:
extern struct _my_struct;
Maxwell Chen People say "No news is good news". Then, no bug is good bug!?
-
Hello all, I know this is not a C list but maybe you can give me some help. Here is my problem : I have this structure defined in file aaa.h typedef struct _my_struct{ char str1[100]; char str2[100]; } my_struct; in the file bbb.c I have a function like this : void my_function(int x, my_struct *p_my_struct) { ... /* doing something*/ } in the coresponding header bbb.h I have the declaration of thefunction : void my_function(int x, my_struct *p_my_struct); how can I declare my_struct in bbb.h as being external ? I've used "external struct my_struct" but it's not compiling. Later I've tried out all the possible combinations but still not compiling. I don't want to include aaa.h in bbb.h TIA, Thomas
Hi, you probebly want to construct my_struct in one .c file and then use it in another .c file. So if I correct the Wright way to do this: 1. First construct your my_struct in one .c file (lets say it will be call "TheStruct”). Example : my_struct TheStruct; 2. Now after you know you construct you my_struct go to other .c file And declare on top of it "extern my_struct TheStruct;" TheStruct is the same my_struct you construct in 1. No Need To do that in .h files ;) Aizik Yair Software Engineer
-
Hello all, I know this is not a C list but maybe you can give me some help. Here is my problem : I have this structure defined in file aaa.h typedef struct _my_struct{ char str1[100]; char str2[100]; } my_struct; in the file bbb.c I have a function like this : void my_function(int x, my_struct *p_my_struct) { ... /* doing something*/ } in the coresponding header bbb.h I have the declaration of thefunction : void my_function(int x, my_struct *p_my_struct); how can I declare my_struct in bbb.h as being external ? I've used "external struct my_struct" but it's not compiling. Later I've tried out all the possible combinations but still not compiling. I don't want to include aaa.h in bbb.h TIA, Thomas
Since you used a typedef you need not define that struct external in a function declaration. Only make sure you include the corresponding header file. external refers to objects, not to types. Best regards, Alexandru Savescu
-
Hi, you probebly want to construct my_struct in one .c file and then use it in another .c file. So if I correct the Wright way to do this: 1. First construct your my_struct in one .c file (lets say it will be call "TheStruct”). Example : my_struct TheStruct; 2. Now after you know you construct you my_struct go to other .c file And declare on top of it "extern my_struct TheStruct;" TheStruct is the same my_struct you construct in 1. No Need To do that in .h files ;) Aizik Yair Software Engineer
Hello Aizik, Thank you for your answer. Unofrtunatelly it's still not working. Here is the code : /* Main.c */ extern struct my_struct *TheStruct; void main() { } int my_function(int x, my_struct *p_my_struct){ printf("inside my_function\n"); } /* Main.h */ int my_function(int x, my_struct *p_my_struct); /* Second.c */ #include "Second.h" my_struct *TheStruct; /* Second.h */ typedef struct _my_struct{ char str1[100]; char str2[100]; } my_struct; Thank you very much, Thomas
-
Since you used a typedef you need not define that struct external in a function declaration. Only make sure you include the corresponding header file. external refers to objects, not to types. Best regards, Alexandru Savescu
-
****Alexandru Savescu wrote: Only make sure you include the corresponding header file Problem is that I don't want to include the header in order to avoid some circular references. Thank you, Thomas
-
In order to avoid ciruclar reference you must use the #ifdef declarations [code] #ifndef __HEADER_I_DONT_WANT_TO_BE_CIRCULAR #define __HEADER_I_DONT_WANT_TO_BE_CIRCULAR .... // all this header declarations #endif [/code] Best regards, Alexandru Savescu