Originally posted by sss555
extern with arrays of structure
Collapse
X
-
Originally posted by horace1in 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?
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.Comment
-
Originally posted by sss555I 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.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 << "in file2 id = " << myList_[i].id << endl; myList_[i].value=i*100; } }
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(); for(int i=0;i<MAX_NO; i++) { cout << "in file 1 id= " << myList_[i].id << " value = " << myList_[i].value << endl; } system("PAUSE"); return EXIT_SUCCESS; }
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?Comment
-
Originally posted by horace1in 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.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 << "in file2 id = " << myList_[i].id << endl; myList_[i].value=i*100; } }
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(); for(int i=0;i<MAX_NO; i++) { cout << "in file 1 id= " << myList_[i].id << " value = " << myList_[i].value << endl; } system("PAUSE"); return EXIT_SUCCESS; }
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 suggestionsComment
Comment