473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extern with arrays of structure

10 New Member
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.
Mar 17 '07
18 9345
AdrianH
1,251 Recognized Expert Top Contributor
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
Mar 18 '07 #11
sss555
10 New Member
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
sss555
10 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
sss555
10 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
sss555
10 New Member
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
horace1
1,510 Recognized Expert Top Contributor
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
sss555
10 New Member
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

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

Similar topics

7
9044
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this: extern char muser, mpass; However, in one of these functions outside of main.c I need to snprintf into these buffers. Usually when using snprintf I call it like this: snprintf(muser, sizeof muser, "text");
33
3878
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
1
3107
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
7
329
by: balasam | last post by:
Dear friend, I am having Linux platform.I am declaring the array of structure in one file and how can access the values of the array of structure in another file. Using " extern " ,to extern the structure but when i access the values of the array of structure ,the compilers displays the error message. Program :
10
5002
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
7
454
by: ruffiano | last post by:
I have a file called first.cpp that contains the following declarations: static const char *MY_STRING1 = "My first string"; static const char *MY_STRING2 = "My second string" If I wanted to use MY_STRING1 and MY_STRING2 in a header file, would the following be correct? extern static const char *MY_STRING1; extern static const char *MY_STRING2;
6
5900
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example will prove useful. structure MyStruct { // this is a complicated struct declaration in the sense
6
3043
by: Scoots | last post by:
I know the usually applied workaround for multiple definitions of header files, but I have a problem on this one. This time, I can't just ifndef the header file that defines my structure. So I have two classes that I've managed to dodge around this problem for a while, but now I need the include in both classes. This wasn't a problem until now, when I'm trying to declare an stl map with a structure as a data type.
4
6025
by: sharat | last post by:
Hi all. I am writing a a code in c++ . i have defined a global structure in a header file(user define header file) say headerfile1.h and declared a class in file2.h which is having some public funtion which returns the struct variable and definition of the fun in in file1.cpp . I am getting the error in compilation that "struct_var' has incomplete type and not allowed and return type is incoplete. If i am trying to return the structure...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.