By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

extern with arrays of structure

P: 10
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,myList_[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.
Mar 17 '07 #1
Share this Question
Share on Google+
18 Replies


Expert 100+
P: 1,510
did your code compile?
there is a ; missing in
Expand|Select|Wrap|Line Numbers
  1. struct Mylistdetails {
  2. int id;
  3. int value;
  4. };  // missing ;
and when including a file in the current directory it should be in ""
e.g. file2.cc
Expand|Select|Wrap|Line Numbers
  1. #include "file1.h"
  2.  
  3. extern struct Mylistdetails myList_[MAX_NO];
  4.  
you could put the extren declaration in the header file
Mar 17 '07 #2

P: 10
did your code compile?
there is a ; missing in
Expand|Select|Wrap|Line Numbers
  1. struct Mylistdetails {
  2. int id;
  3. int value;
  4. };  // missing ;
It compiled without any problem.


and when including a file in the current directory it should be in ""
e.g. file2.cc
Expand|Select|Wrap|Line Numbers
  1. #include "file1.h"
  2.  
  3. extern struct Mylistdetails myList_[MAX_NO];
  4.  
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).Still doesn't work.

Thanks for your attention.
Mar 17 '07 #3

Expert 100+
P: 1,510
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).Still doesn't work.

Thanks for your attention.
I guess you are on a unix system, try
Expand|Select|Wrap|Line Numbers
  1. #include "direct/file1.h"
assuming you have a directory direct in your current directory which contains the file1.h
Mar 17 '07 #4

P: 10
I guess you are on a unix system, try
Expand|Select|Wrap|Line Numbers
  1. #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
Mar 17 '07 #5

Expert 100+
P: 1,510
Expand|Select|Wrap|Line Numbers
  1. #include <filename>                              // include file from 'standard' place 
  2.  
the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,.

Expand|Select|Wrap|Line Numbers
  1. #include "filename"       // include file from current directory else 'standard' place 
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file2.cpp
  13.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include "file1.h"
  4.  
  5. void setup()
  6. {
  7. for(int i=0;i<MAX_NO; i++)
  8.     myList_[i].id=i;
  9. }
  10.  
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8. struct Mylistdetails myList_[MAX_NO];
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     setup();
  13.     for(int i=0;i<MAX_NO; i++)
  14.         cout << myList_[i].id << endl;
  15.     return EXIT_SUCCESS;
  16. }
  17.  
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
Mar 17 '07 #6

P: 10
Expand|Select|Wrap|Line Numbers
  1. #include <filename>                              // include file from 'standard' place 
  2.  
the file is assumed to be in a 'standard' directory in the file system which the C/C++ compiler automatically searches,.

Expand|Select|Wrap|Line Numbers
  1. #include "filename"       // include file from current directory else 'standard' place 
  2.  
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
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file2.cpp
  13.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include "file1.h"
  4.  
  5. void setup()
  6. {
  7. for(int i=0;i<MAX_NO; i++)
  8.     myList_[i].id=i;
  9. }
  10.  
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8. struct Mylistdetails myList_[MAX_NO];
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     setup();
  13.     for(int i=0;i<MAX_NO; i++)
  14.         cout << myList_[i].id << endl;
  15.     return EXIT_SUCCESS;
  16. }
  17.  
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.
Mar 17 '07 #7

Expert 100+
P: 1,510
try this which passes data from file1.cpp to file2.cpp
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file1.cpp
  13. void print();   // function prototype - function in file2.cpp
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void print()
  10. {
  11.     for(int i=0;i<MAX_NO; i++)
  12.       cout << myList_[i].id << endl;
  13. }
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include "file1.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Mylistdetails myList_[MAX_NO];
  10.  
  11. void setup()
  12. {
  13. for(int i=0;i<MAX_NO; i++)
  14.     myList_[i].id=i;
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     setup();
  21.     print();
  22.     return EXIT_SUCCESS;
  23. }
  24.  
Mar 18 '07 #8

P: 10
try this which passes data from file1.cpp to file2.cpp
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file1.cpp
  13. void print();   // function prototype - function in file2.cpp
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void print()
  10. {
  11.     for(int i=0;i<MAX_NO; i++)
  12.       cout << myList_[i].id << endl;
  13. }
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include "file1.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Mylistdetails myList_[MAX_NO];
  10.  
  11. void setup()
  12. {
  13. for(int i=0;i<MAX_NO; i++)
  14.     myList_[i].id=i;
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     setup();
  21.     print();
  22.     return EXIT_SUCCESS;
  23. }
  24.  

I still get this error while trying to compile.

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



Thank you
Mar 18 '07 #9

Expert 100+
P: 1,510
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
Mar 18 '07 #10

AdrianH
Expert 100+
P: 1,251
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,myList_[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
Mar 18 '07 #11

P: 10
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
Mar 18 '07 #12

P: 10
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.
Mar 18 '07 #13

Expert 100+
P: 1,510
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?
Mar 18 '07 #14

P: 10
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.
Mar 18 '07 #15

Expert 100+
P: 1,510
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.
in my last post function setup() in File1.cpp put data into myList[] and function print() in file2.cpp extracted and displayed it. Is you problem more complex than this, e.g. do you have other data structures to share?
Mar 18 '07 #16

P: 10
in my last post function setup() in File1.cpp put data into myList[] and function print() in file2.cpp extracted and displayed it. Is you problem more complex than this, e.g. do you have other data structures to share?
I simply require the array instances in file1.cc to be reflected in file2.cc.
The problem I see is whenever I define extern in file1.h without even trying to retrieve from file2.cc, the values indicated in original arrays of struct go haywire.
Mar 18 '07 #17

Expert 100+
P: 1,510
I simply require the array instances in file1.cc to be reflected in file2.cc.
The problem I see is whenever I define extern in file1.h without even trying to retrieve from file2.cc, the values indicated in original arrays of struct go haywire.
in this version function setup() in File1.cpp puts data into myList[], function print() in file2.cpp extracts and displays it and changes it, when it is displayed in file1.cpp.
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file1.cpp
  13. void print();   // function prototype - function in file2.cpp
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void print()
  10. {
  11.     for(int i=0;i<MAX_NO; i++)
  12.       {
  13.       cout << "in file2 id = " <<  myList_[i].id << endl;
  14.       myList_[i].value=i*100;
  15.       }
  16. }
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include "file1.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Mylistdetails myList_[MAX_NO];
  10.  
  11. void setup()
  12. {
  13. for(int i=0;i<MAX_NO; i++)
  14.     myList_[i].id=i;
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     setup();
  21.     print();
  22.     for(int i=0;i<MAX_NO; i++)
  23.       {
  24.       cout << "in file 1 id= " << myList_[i].id << 
  25.               " value = " <<  myList_[i].value << endl;
  26.       }
  27.  
  28.     system("PAUSE");
  29.     return EXIT_SUCCESS;
  30. }
a run gives
in file2 id = 0
in file2 id = 1
in file2 id = 2
in file2 id = 3
in file2 id = 4
in file2 id = 5
in file2 id = 6
in file2 id = 7
in file2 id = 8
in file2 id = 9
in file 1 id= 0 value = 0
in file 1 id= 1 value = 100
in file 1 id= 2 value = 200
in file 1 id= 3 value = 300
in file 1 id= 4 value = 400
in file 1 id= 5 value = 500
in file 1 id= 6 value = 600
in file 1 id= 7 value = 700
in file 1 id= 8 value = 800
in file 1 id= 9 value = 900
Press any key to continue . . .

could you post the code which is giving you problems?
Mar 18 '07 #18

P: 10
in this version function setup() in File1.cpp puts data into myList[], function print() in file2.cpp extracts and displays it and changes it, when it is displayed in file1.cpp.
Expand|Select|Wrap|Line Numbers
  1. // file1.h
  2.  
  3. struct Mylistdetails {
  4. int id;
  5. int value;
  6. };
  7.  
  8. #define MAX_NO 10
  9.  
  10. extern struct Mylistdetails myList_[MAX_NO];
  11.  
  12. void setup();   // function prototype - function in file1.cpp
  13. void print();   // function prototype - function in file2.cpp
  14.  
  15.  
Expand|Select|Wrap|Line Numbers
  1. // file2.cpp
  2.  
  3. #include <iostream>
  4. #include "file1.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void print()
  10. {
  11.     for(int i=0;i<MAX_NO; i++)
  12.       {
  13.       cout << "in file2 id = " <<  myList_[i].id << endl;
  14.       myList_[i].value=i*100;
  15.       }
  16. }
Expand|Select|Wrap|Line Numbers
  1. // file1.cpp
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include "file1.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Mylistdetails myList_[MAX_NO];
  10.  
  11. void setup()
  12. {
  13. for(int i=0;i<MAX_NO; i++)
  14.     myList_[i].id=i;
  15. }
  16.  
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     setup();
  21.     print();
  22.     for(int i=0;i<MAX_NO; i++)
  23.       {
  24.       cout << "in file 1 id= " << myList_[i].id << 
  25.               " value = " <<  myList_[i].value << endl;
  26.       }
  27.  
  28.     system("PAUSE");
  29.     return EXIT_SUCCESS;
  30. }
a run gives
in file2 id = 0
in file2 id = 1
in file2 id = 2
in file2 id = 3
in file2 id = 4
in file2 id = 5
in file2 id = 6
in file2 id = 7
in file2 id = 8
in file2 id = 9
in file 1 id= 0 value = 0
in file 1 id= 1 value = 100
in file 1 id= 2 value = 200
in file 1 id= 3 value = 300
in file 1 id= 4 value = 400
in file 1 id= 5 value = 500
in file 1 id= 6 value = 600
in file 1 id= 7 value = 700
in file 1 id= 8 value = 800
in file 1 id= 9 value = 900
Press any key to continue . . .

could you post the code which is giving you problems?

when extern storage class is called, could the arrays of structure hold multiple instances..

Because this arrays of file1.cc hold neighbor information of nodes (time varying) and when extern is called that behavior is no more there.

Could send 'you' the files .Thanks for your suggestions
Mar 19 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.