Go to Post Students design and build it, the engineers/mentors are there to guide us when we stray off the path. A great way to learn, a great way to mentor, a great way to grow. - chellyzee93 [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 17-01-2004, 07:29
D.Viddy's Avatar
D.Viddy D.Viddy is offline
Registered User
AKA: Dylan Vester
FRC #3176 (Purple Precision)
Team Role: Programmer
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Camby, IN
Posts: 44
D.Viddy is an unknown quantity at this point
Question Using the extern keyword.

I'm trying to make a variable in the user_routines_fast.c file that can be accessed in the user_routines.c file. How do I do this. I think it uses the extern keyword? Am I wrong? If I'm not. How do I properly use it. Thanks.
__________________
.................................................. .........
Dylan Vester - (Programmer)
Running a P4 1.8 Ghz
Team 998, We can't win...
.................................................. .........
  #2   Spotlight this post!  
Unread 17-01-2004, 08:06
m0rph3us's Avatar
m0rph3us m0rph3us is offline
Registered User
AKA: Andre
#0293 (Spike)
Team Role: Programmer
 
Join Date: Feb 2003
Location: Hopewell, NJ
Posts: 32
m0rph3us will become famous soon enough
Send a message via ICQ to m0rph3us Send a message via AIM to m0rph3us Send a message via MSN to m0rph3us Send a message via Yahoo to m0rph3us
Re: Using the extern keyword.

Yes the you can use the keyword extern like the following:

Code:
//user_routines_fast.c
int foo;
...


//user_routines.c
extern int foo;

void test()
{
foo=2;
}
But the more professional way of doing this is by declaring the variable in user_routines.h where than you don't have to use extern but rather only have to include the file user_routines.h (as it is already done in the default code)
__________________
Andre
Team S.P.I.K.E.293
  #3   Spotlight this post!  
Unread 17-01-2004, 10:45
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Using the extern keyword.

no, do not define the variable in user_routines.h - this does not actually share the variable. instead, you get two copies, one in each file. (remember, putting something in an include file is just like inserting that text into each file in which you #include it). if you look at the user_routines.h file, you will notice that there is not a single actual variable defined there - there's a reason for that.

extern is probably the way to go here, just declare the variable in one file, then copy the definition to the other but tack "extern " on the front. if you're passing more than one thing back and forth, you might consider creating a struct to keep all of the info in one single variable that can be extern-ed (having more than one or two global variables looks messy )

if you wanted to be ambitious, you could modify main.c so that Process_Data_From_Local_IO() and Process_Data_From_Master_uP() could return and be called with a value, thus passing the info that way (using a struct)
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #4   Spotlight this post!  
Unread 18-01-2004, 00:31
SeanCassidy's Avatar
SeanCassidy SeanCassidy is offline
Antiregistered User
#0263 (Aftershock)
Team Role: Programmer
 
Join Date: Oct 2003
Location: Holtsville, NY
Posts: 37
SeanCassidy is an unknown quantity at this point
Re: Using the extern keyword.

Hmm, when the variable is changed in user_routines.c, do you have to extern it back in u_r_f.c? Like so:

Code:
/*user_routines_fast.c*/
int foo = 263;
extern user_routines_function();    /*Is this extern needed?*/
extern foo = foo - 1;               /*Do we need the extern now,
                                     *because it was modified in u_r.c?*/

/*user_routines.c*/
user_routines_function()
{
  extern foo = foo + 3;
}
  #5   Spotlight this post!  
Unread 18-01-2004, 02:07
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
Re: Using the extern keyword.

Quote:
Originally Posted by SeanCassidy
Hmm, when the variable is changed in user_routines.c, do you have to extern it back in u_r_f.c? Like so:

Code:
/*user_routines_fast.c*/
int foo = 263;
extern user_routines_function();    /*Is this extern needed?*/
extern foo = foo - 1;               /*Do we need the extern now,
                                     *because it was modified in u_r.c?*/

/*user_routines.c*/
user_routines_function()
{
  extern foo = foo + 3;
}
extern should never be used inside an expression like that. It is only used in declarations made at the file-scope level. Thus, you just do stuff like this:

/*user_routines_fast.c*/
int foo;

void myfunc(){
foo=foo+1;
}

/*user_routines.c*/
extern int foo;

void myfunc2(){
foo=foo+3;
}


They keyword extern is NEVER needed with function prototypes, as they are extern implicitly.

-Rob
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #6   Spotlight this post!  
Unread 18-01-2004, 02:08
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Using the extern keyword.

extern is only used in the first declaration of variable in a file - treat it just as if you were declaring the variable normally, extern just says "this variable was originally declared in another file, and the same copy should be used here".

also, remember that exactly one file should have the variable declared without "extern" - all of the other files extern this original declaration.

Finally, remember that extern only really works for global variables. So make sure your extern-ed variables are declared outside of any function calls.

--edit-- lol, it happened again.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #7   Spotlight this post!  
Unread 20-01-2004, 12:48
jeremy562 jeremy562 is offline
Engineer
#0562 (SPARK 562)
 
Join Date: Feb 2002
Location: Fitchburg, MA
Posts: 74
jeremy562 is an unknown quantity at this point
Send a message via AIM to jeremy562
Re: Using the extern keyword.

I hate globals, and I hate the extern keyword.

If you can pass the variable as a parameter to any function where it's needed, do it that way. It's much cleaner, and less error-prone.
__________________
SPARK 562: Students Pursuing Applied Robotics Knowledge

2006 BAE Granite State Regional: Finalists with 319 and 176
2006 BAE Granite State Regional: Winners of Motorola Quality Award
2004 BAE Granite State Regional: 7th Seed
2004 Mayhem on the Merrimack Champions! Thanks, 61 and 1289!
  #8   Spotlight this post!  
Unread 20-01-2004, 21:22
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Using the extern keyword.

only argument i'd have against that is in this case, that would mean modifying the main.c loop and code, which a beginner programmer might not want to do (however, I agree for the most part and would/will probably implement it in such a way myself).
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
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


All times are GMT -5. The time now is 19:56.

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