extern with arrays of structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sss555
    New Member
    • Mar 2007
    • 10

    extern with arrays of structure

    Hi,

    I tried to share some data between 2 .cc files hence declared the arrays of structure as extern in one of the .cc files.

    However I get abnormal entries in the original array entries otherwise I wouldn't have got.

    file1.h
    ---------
    struct Mylistdetails {
    int id;
    int value;
    }


    file1.cc
    ----------
    struct Mylistdetails myList_[MAX_NO];


    file2.cc
    -----------
    #include <file1.h>

    extern struct Mylistdetails myList_[MAX_NO];


    The purpose here is to call these array data into the file2.Here,myLi st_[i].id and myList_[i].value are just unexpected once declared as 'extern'.If not used this extern method the expected values are observed once only file2 is being used.

    Could you kindly specify the behaviour of this.Many thanks for your advise on how to tackle such a problem.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    did your code compile?
    there is a ; missing in
    Code:
    struct Mylistdetails {
    int id;
    int value;
    };  // missing ;
    and when including a file in the current directory it should be in ""
    e.g. file2.cc
    Code:
    #include "file1.h"
    
    extern struct Mylistdetails myList_[MAX_NO];
    you could put the extren declaration in the header file

    Comment

    • sss555
      New Member
      • Mar 2007
      • 10

      #3
      Originally posted by horace1
      did your code compile?
      there is a ; missing in
      Code:
      struct Mylistdetails {
      int id;
      int value;
      };  // missing ;
      It compiled without any problem.


      and when including a file in the current directory it should be in ""
      e.g. file2.cc
      Code:
      #include "file1.h"
      
      extern struct Mylistdetails myList_[MAX_NO];
      you could put the extren declaration in the header file
      well it is not in the current directory, so i specified the path
      #include <direct/file1.h>

      Yes I tried putting extern declaration in the heade file too(file1.h).St ill doesn't work.

      Thanks for your attention.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by sss555
        well it is not in the current directory, so i specified the path
        #include <direct/file1.h>

        Yes I tried putting extern declaration in the heade file too(file1.h).St ill doesn't work.

        Thanks for your attention.
        I guess you are on a unix system, try
        Code:
        #include "direct/file1.h"
        assuming you have a directory direct in your current directory which contains the file1.h

        Comment

        • sss555
          New Member
          • Mar 2007
          • 10

          #5
          Originally posted by horace1
          I guess you are on a unix system, try
          Code:
          #include "direct/file1.h"
          assuming you have a directory direct in your current directory which contains the file1.h
          I'm working on Linux, and it works normally in #include<direct/file1.h> style.

          Could you explain me how could I use, extern with arrays of structure?

          Thank you

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Code:
            #include <filename>                              // include file from 'standard' place
            the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,.

            Code:
            #include "filename"       // include file from current directory else 'standard' place
            The second form of #include (filename enclosed in "") searches the current directory for filename and then, if it is not found, the 'standard' directory.

            which you use it depends where your direct/file1.h file is - in the 'standard' place or relative to the current directory?

            try the following
            Code:
            // file1.h
            
            struct Mylistdetails {
            int id;
            int value;
            };
            
            #define MAX_NO 10
            
            extern struct Mylistdetails myList_[MAX_NO];
            
            void setup();   // function prototype - function in file2.cpp
            Code:
            // file2.cpp
            
            #include "file1.h"
            
            void setup()
            {
            for(int i=0;i<MAX_NO; i++)
                myList_[i].id=i;
            }
            Code:
            // file1.cpp
            #include <cstdlib>
            #include <iostream>
            #include "file1.h"
            
            using namespace std;
            
            struct Mylistdetails myList_[MAX_NO];
            
            int main(int argc, char *argv[])
            {
                setup();
                for(int i=0;i<MAX_NO; i++)
                    cout << myList_[i].id << endl;
                return EXIT_SUCCESS;
            }
            when i build this
            g++ file1.cpp file2.cpp

            and run it I get
            ./a
            0
            1
            2
            3
            4
            5
            6
            7
            8
            9

            Comment

            • sss555
              New Member
              • Mar 2007
              • 10

              #7
              Originally posted by horace1
              Code:
              #include <filename>                              // include file from 'standard' place
              the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,.

              Code:
              #include "filename"       // include file from current directory else 'standard' place
              The second form of #include (filename enclosed in "") searches the current directory for filename and then, if it is not found, the 'standard' directory.

              which you use it depends where your direct/file1.h file is - in the 'standard' place or relative to the current directory?

              try the following
              Code:
              // file1.h
              
              struct Mylistdetails {
              int id;
              int value;
              };
              
              #define MAX_NO 10
              
              extern struct Mylistdetails myList_[MAX_NO];
              
              void setup();   // function prototype - function in file2.cpp
              Code:
              // file2.cpp
              
              #include "file1.h"
              
              void setup()
              {
              for(int i=0;i<MAX_NO; i++)
                  myList_[i].id=i;
              }
              Code:
              // file1.cpp
              #include <cstdlib>
              #include <iostream>
              #include "file1.h"
              
              using namespace std;
              
              struct Mylistdetails myList_[MAX_NO];
              
              int main(int argc, char *argv[])
              {
                  setup();
                  for(int i=0;i<MAX_NO; i++)
                      cout << myList_[i].id << endl;
                  return EXIT_SUCCESS;
              }
              when i build this
              g++ file1.cpp file2.cpp

              and run it I get
              ./a
              0
              1
              2
              3
              4
              5
              6
              7
              8
              9

              In fact I want to retrieve file1 data into file2.If I understand your code correctly it does the other way round.

              I tried this way and when I compile I get the following error

              file1.h:270: error: storage class specified for 'myList_'



              Thank you for your suggestions.

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                try this which passes data from file1.cpp to file2.cpp
                Code:
                // file1.h
                
                struct Mylistdetails {
                int id;
                int value;
                };
                
                #define MAX_NO 10
                
                extern struct Mylistdetails myList_[MAX_NO];
                
                void setup();   // function prototype - function in file1.cpp
                void print();   // function prototype - function in file2.cpp
                Code:
                // file2.cpp
                
                #include <iostream>
                #include "file1.h"
                
                using namespace std;
                
                
                void print()
                {
                    for(int i=0;i<MAX_NO; i++)
                      cout << myList_[i].id << endl;
                }
                Code:
                // file1.cpp
                
                #include <cstdlib>
                #include <iostream>
                #include "file1.h"
                
                using namespace std;
                
                struct Mylistdetails myList_[MAX_NO];
                
                void setup()
                {
                for(int i=0;i<MAX_NO; i++)
                    myList_[i].id=i;
                }
                
                
                int main(int argc, char *argv[])
                {
                    setup();
                    print();
                    return EXIT_SUCCESS;
                }

                Comment

                • sss555
                  New Member
                  • Mar 2007
                  • 10

                  #9
                  Originally posted by horace1
                  try this which passes data from file1.cpp to file2.cpp
                  Code:
                  // file1.h
                  
                  struct Mylistdetails {
                  int id;
                  int value;
                  };
                  
                  #define MAX_NO 10
                  
                  extern struct Mylistdetails myList_[MAX_NO];
                  
                  void setup();   // function prototype - function in file1.cpp
                  void print();   // function prototype - function in file2.cpp
                  Code:
                  // file2.cpp
                  
                  #include <iostream>
                  #include "file1.h"
                  
                  using namespace std;
                  
                  
                  void print()
                  {
                      for(int i=0;i<MAX_NO; i++)
                        cout << myList_[i].id << endl;
                  }
                  Code:
                  // file1.cpp
                  
                  #include <cstdlib>
                  #include <iostream>
                  #include "file1.h"
                  
                  using namespace std;
                  
                  struct Mylistdetails myList_[MAX_NO];
                  
                  void setup()
                  {
                  for(int i=0;i<MAX_NO; i++)
                      myList_[i].id=i;
                  }
                  
                  
                  int main(int argc, char *argv[])
                  {
                      setup();
                      print();
                      return EXIT_SUCCESS;
                  }

                  I still get this error while trying to compile.

                  file1.h:270: error: storage class specified for ‘myList_’



                  Thank you

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    Originally posted by sss555
                    I still get this error while trying to compile.

                    file1.h:270: error: storage class specified for ‘myList_’



                    Thank you
                    I have run the code in my last post on Linux and Windows (using Cygwin). I compile with
                    g++ file1.cpp file2.cpp

                    then execute the file a.out or a.exe OK

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by sss555
                      Hi,

                      I tried to share some data between 2 .cc files hence declared the arrays of structure as extern in one of the .cc files.

                      However I get abnormal entries in the original array entries otherwise I wouldn't have got.

                      file1.h
                      ---------
                      struct Mylistdetails {
                      int id;
                      int value;
                      }


                      file1.cc
                      ----------
                      struct Mylistdetails myList_[MAX_NO];


                      file2.cc
                      -----------
                      #include <file1.h>

                      extern struct Mylistdetails myList_[MAX_NO];


                      The purpose here is to call these array data into the file2.Here,myLi st_[i].id and myList_[i].value are just unexpected once declared as 'extern'.If not used this extern method the expected values are observed once only file2 is being used.

                      Could you kindly specify the behaviour of this.Many thanks for your advise on how to tackle such a problem.
                      If you are getting unexpected values, perhaps your MAX_NO is not defined the same value in each file. I see no reference to it.


                      Adrian

                      Comment

                      • sss555
                        New Member
                        • Mar 2007
                        • 10

                        #12
                        Originally posted by horace1
                        I have run the code in my last post on Linux and Windows (using Cygwin). I compile with
                        g++ file1.cpp file2.cpp

                        then execute the file a.out or a.exe OK
                        I managed to compile it this time, but the ultimate result is the same as I got when I declared extern struct in both the .cc files (like in my first posting)

                        Thanks

                        Comment

                        • sss555
                          New Member
                          • Mar 2007
                          • 10

                          #13
                          Originally posted by sss555
                          I managed to compile it this time, but the ultimate result is the same as I got when I declared extern struct in both the .cc files (like in my first posting)

                          Thanks
                          What I observe is when declared as extern, it maintains a single instance hence overwrites the array, whereas otherwise it maintains multiple instances giving each value,id pair.
                          If I'm to elaborate a bit:
                          This i do to maintain a neighbor table that corresponds to each node, so when
                          you run file2.cc alone(without extern), each node has it's own neighbor table(with corresponding id,value pair of its neighbors)

                          when I call extern, it gives a list of entries which I can not figureout.The onlything I could see is starting from the same initial set of {id,val} it keeps on populating the same array until you reach the MAX_NO of neighbors.

                          So there's no correlation of nodes and their neighbor table.

                          Comment

                          • horace1
                            Recognized Expert Top Contributor
                            • Nov 2006
                            • 1510

                            #14
                            by using extern you have one struct Mylistdetails array shared by both file1.cpp and file2.cpp. Is this what you want?
                            or do you want two seperate arrays, one in file1.cpp and the other in file2.cpp?
                            I don't understand what you are trying to do?

                            Comment

                            • sss555
                              New Member
                              • Mar 2007
                              • 10

                              #15
                              Originally posted by horace1
                              by using extern you have one struct Mylistdetails array shared by both file1.cpp and file2.cpp. Is this what you want?
                              or do you want two seperate arrays, one in file1.cpp and the other in file2.cpp?
                              I don't understand what you are trying to do?
                              Well, I want the file2.cc to be capable of accessing information maintained by arrays in file1.cc.

                              i.e file1 maintains {id,val} information for each node;and file2 tries to retrieve this information.

                              Thanks for your questions and suggestions.

                              Comment

                              Working...