Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problems with "external struct"

Problems with "external struct"

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    M A A 3 Replies Last reply
    0
    • L Lost User

      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

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      Simply write:

      extern struct _my_struct;

      Maxwell Chen People say "No news is good news". Then, no bug is good bug!?

      1 Reply Last reply
      0
      • L Lost User

        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

        A Offline
        A Offline
        Aizik Yair
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • L Lost User

          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

          A Offline
          A Offline
          Alexandru Savescu
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • A Aizik Yair

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Alexandru Savescu

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              ****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

              L 1 Reply Last reply
              0
              • L Lost User

                ****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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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

                L 1 Reply Last reply
                0
                • L Lost User

                  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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You're perfectlly right Alexandru; this is the way I always do; yet my problem is not solved. When using a pointer to the structure compilation is failling. TIA, Thomas

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups