|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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.
|
|
#2
|
||||
|
||||
|
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;
}
|
|
#3
|
||||
|
||||
|
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) |
|
#4
|
||||
|
||||
|
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
|
||||||
|
||||||
|
Re: Using the extern keyword.
Quote:
/*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 |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
||||
|
||||
|
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).
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|