![]() |
Static Variables
If I want to define a variable inside of a function and want it to be available to all functions in the .c file, will static do it?
Would this work? void FunctitonWhatever1(void) { static int bob=1; /*Note the static and the =1*/ CallerFuction(); } void CallerFunction(void) { int bill=0; /*Only need it in this function*/ bill=2+bob; } |
Re: Static Variables
Quote:
If you need to access a variable from many functions you can either make it a global variable or pass it around as part of the function calls. |
Re: Static Variables
Declare it at the top of a file, then in other files put
Code:
extern int bob; |
Re: Static Variables
Quote:
MyFunction.c void declarations(void) { int bob=2; } void hi(void) { int bill=0; extern int bob; bill=bob+2 } I need to define a variable in a function and make it available to everywhere in the rest of the file. |
Re: Static Variables
No, there is no need for the declarations function. Just declare the variable outside of all functions. This will make that variable a global variable so it will be available to all the functions in the file. Also the extern int declaration is not needed when the variable is a global variable.
Your code with the above modifications would look like this: Code:
MyFunction.c |
Re: Static Variables
declare the variable(s) you want to use outside of a function (outside any brackets).
Then, move the extern line(s) into a .h file (user_routines.h) The extern lines are optional, and are only needed if you want to use them in more than one file (user_routines.c and user_routines_fast.c for autonomous) After that, you can read and write to them from any function: user_routines.c Code:
#include "user_routines.h"Code:
... |
Re: Static Variables
So, just to recap. I can use extern in the header and declare them at the TOP of userroutines.c. They will be defined once and available to all of userroutines.c as well as anywhere else.
I thought C was run-by-call only. Does C have the exception that it will run everything outside of functions once and then goto rbc only? My original plan was to delaclare my variables withing userinitialization() to make sure they run get declared once. But if I can just do it outside of all functions and add some stuff to .h and have it work; it is as good as putting it in userinitialization() |
Re: Static Variables
Quote:
Lynn (D) - Team Voltage Software |
Re: Static Variables
Actually, you can not put explicit code outside of functions, but you are allowed to put initial values for all variables. These must be constants, and can only be either arrays like (1,45,3,21,4,5...} or integers like 2.
They can also include basic expressions like: Code:
#define M_PI 3.14159265358979But, no actual code is allowed, and the order in which variables are initialized is also undefined. So you can and should only use this to initalize defaults. If you do not do this, then the variables will be uninitialized, meaning that they could start out with any value. If you want code to run, then put all of that in your User_Initialization(). Beware that no pwms or inputs or control inputs can be read or written in the initialization function. |
| All times are GMT -5. The time now is 03:00. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi