![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
accessing a variable across files
i have a 2d int array that i want to access from across files.
i have heard of making a variable extern but don't really know what that means. If i put it in a .h file and then include that .h file i get an error saying more then 1 has been defined. Is there a way without classes for me to be able to access the same 2d int array from more then one file. The reason i dont want a class is because this is the only file i need to access across files. Functions in the other file modify this 2d int array so if i could have it set up somewhere and then let all the other functions access it by including a .h file or something i think that would be ideal. So basically i want to make a int 2d array global across files. I think there is a way to check if something is already defined in a header file is that what im looking for maybe? Thanks. |
|
|
|
|
|
#2 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
Re: accessing a variable across files
The extern keyword means that the variable will be actually declared somewhere else. So in the *.h file use extern and in one, and only one, *.c or *.cpp file declare it again but without the extern keyword. You don't have to put it in a *.h file, instead you can do it like this:
// A.cpp extern int ay[2]; // blabla // B.cpp int ay[2]; // blabla
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: accessing a variable across files
so by using extern in one file and then just declaring it normally in another it will be using the same variable in both files right? I want the same 2d int array to be updated from 2 different files so does this make 2 copies of the array or just a way to access the same array from 2 different files? Actually how then would i access the same array from 3 or more different files. You said i could delcare it in 1 other place is there a way i would be able to declare it in more then 2 places?
|
|
|
|
|
|
#4 |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4
![]() |
Re: accessing a variable across files
When you declare it as extern in all but one *.cpp file there is only ONE copy of the array regardless of how many *.c or *.cpp files there are. Update the array from one *.cpp file and the changes will be visible in all *.cpp files.
If this is still a little confusing to you, create yourself a little program that has three *.cpp files and test out what I said. Put print statements in each *.cpp file to display the value of the array after it has been changed by that *.cpp file.
__________________
I Like Ike. Vote for Dwight Eisenhower this November. --This message brought to you by the the Procrastinators Club Of America. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 333
Rep Power: 4
![]() |
Re: accessing a variable across files
Oh ok so as long as in each .cpp file you declare it extern then it will use the same one. That makes sense
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Alter many files, all with same variable in them | woocha | PHP | 5 | Jan 17th, 2008 10:26 PM |
| Frequently accessing structure member VS. create a copy variable | kurt | C++ | 6 | Oct 22nd, 2007 1:56 PM |
| Accessing structs in external files. | melchior | C | 5 | Feb 23rd, 2006 1:41 PM |
| Checking source codes of image, audio and video files | on_auc | C++ | 3 | Feb 21st, 2005 8:36 PM |
| shutil.copy corrupting files? | TimL | Python | 0 | Feb 20th, 2005 3:32 PM |