Go to Post Anything that promotes science and technology is okay in my book. - Dan Richardson [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 27-01-2008, 15:03
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
Global variable question

If I create a global variable in one .c file, can I access this variable from another .c file?

I thought so, but I get the error "symbol 'varname' has not been defined."

Thanks,
Nathan
__________________
Save money with PriceWombat, a shopping comparison and price tracking tool (that I made!).

Check out my hobby electronics business!

My former robotics team: Huntington County 4-H Robotics
  #2   Spotlight this post!  
Unread 27-01-2008, 15:09
ay2b's Avatar
ay2b ay2b is offline
Registered User
AKA: Andy
FRC #2928
Team Role: Mentor
 
Join Date: Mar 2004
Rookie Year: 1994
Location: Seattle, WA
Posts: 211
ay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant futureay2b has a brilliant future
Re: Global variable question

In one file, define it like this:
Code:
int foo;
And in the other file, define it like this:
Code:
extern int foo;
In the first file, it will define the variable, and allocate the memory space for it. In the second file, it will define the variable, but know that the memory space for it is external to that module.
__________________

2011 - SD Quarterfinalists (980), LA Quarterfinalists (980)
2010 - LA (2404) Finalists (980), AZ Motorola Quality (980)
2009 - LA Semifinalists (980); Las Vegas Quarterfinalists (980); SD (2404); IRI #1 Seed, Finalist (980)
2008 - SD Quarterfinalists (980), LA Champions (980), LA Rookie Inspiration Award (2404); CalGames Finalists
2007 - So.Cal Finalists (980), SD Quarterfinalists (980); CalGames Finalists
2006 - So.Cal Regional Champion (4), Toronto Judge's Award Day 1 (4)
2005 - SVR Champions, Delphi "Driving Tomorrow's Technology" (980); AZ Xerox Creativity (980); So.Cal Finalists, RadioShack Innovation in Control (980); Championship Archimedes Division Semifinalists; IRI Finalists (980)
2004 - So.Cal Regional Champions, Leadership in Controls (980); AZ GM Industrial Design (980); Championship Galileo Division #2 Seed; IRI Champions
2003 - PNW Semi-finalists (488)
2002 - PNW Finalists (488)
2000 - X-bot / 488 - Mentor / Founder
1994 - Sunny Delight - Driver - champion
  #3   Spotlight this post!  
Unread 27-01-2008, 15:56
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
Re: Global variable question

Great, thanks!

Nathan
__________________
Save money with PriceWombat, a shopping comparison and price tracking tool (that I made!).

Check out my hobby electronics business!

My former robotics team: Huntington County 4-H Robotics
  #4   Spotlight this post!  
Unread 27-01-2008, 18:02
jacobhurwitz jacobhurwitz is offline
Registered User
FRC #0449 (Blair Robot Project)
Team Role: Programmer
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Maryland
Posts: 45
jacobhurwitz has a spectacular aura aboutjacobhurwitz has a spectacular aura aboutjacobhurwitz has a spectacular aura about
Re: Global variable question

A simple way to make sure this works in all files is to add this to a header (.h) file:

Code:
#ifdef DEFINE_VARS
extern int foo;
#endif

#ifndef DEFINE_VARS
#define DEFINE_VARS
int foo;
#endif
So, what does this do? You have some macro, DEFINE_VARS. If it has already been defined, you declare extern int foo. If it hasn't been defined, you define it and declare int foo. This way, foo is external for all files but one.

I hope this helps.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB 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
Global Variable Placement robbrad Programming 1 23-01-2008 00:50
Stupid Variable question.... BillyJ Programming 3 20-02-2006 19:49
Beginning programmer: Variable question HELP!!! Calvin Programming 4 03-02-2006 18:12
Camera variable scope question Validius Programming 1 28-01-2006 09:24


All times are GMT -5. The time now is 09:23.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi