Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 19th, 2008, 1:21 AM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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.
cwl157 is offline   Reply With Quote
Old Jun 19th, 2008, 3:45 AM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jun 19th, 2008, 8:51 AM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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?
cwl157 is offline   Reply With Quote
Old Jun 19th, 2008, 8:18 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 529
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jun 20th, 2008, 12:14 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 333
Rep Power: 4 cwl157 is on a distinguished road
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
cwl157 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC