Go to Post I am thoroughly convinced FIRST is a sport. Convince me otherwise. - Bharat Nain [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 01-03-2005, 23:17
amateurrobotguy's Avatar
amateurrobotguy amateurrobotguy is offline
Lead Programmer/Senior Engineer
no team
 
Join Date: Feb 2005
Rookie Year: 2000
Location: ****
Posts: 136
amateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these parts
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;
}
__________________
I quit FRC over 2 years ago (more if you're reading this past 2010).
  #2   Spotlight this post!  
Unread 01-03-2005, 23:29
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: Static Variables

Quote:
Originally Posted by amateurrobotguy
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?
No, not as you listed it. If you declare a variable inside a function (static or not), it is only available inside that function. The difference is that a static variable declared inside a function will retain its value between calls to that function. So it acts like a global variable but it can only be seen by that function.

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.
  #3   Spotlight this post!  
Unread 02-03-2005, 09:20
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Static Variables

Declare it at the top of a file, then in other files put
Code:
extern int bob;
at the top.
__________________
http://www.mikesorrenti.com/
  #4   Spotlight this post!  
Unread 02-03-2005, 18:48
amateurrobotguy's Avatar
amateurrobotguy amateurrobotguy is offline
Lead Programmer/Senior Engineer
no team
 
Join Date: Feb 2005
Rookie Year: 2000
Location: ****
Posts: 136
amateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these parts
Re: Static Variables

Quote:
Originally Posted by MikeWasHere05
Declare it at the top of a file, then in other files put
Code:
extern int bob;
at the top.
So basically like this.

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.
__________________
I quit FRC over 2 years ago (more if you're reading this past 2010).

Last edited by amateurrobotguy : 02-03-2005 at 19:05.
  #5   Spotlight this post!  
Unread 02-03-2005, 19:27
JoelP JoelP is offline
whats the P for? Pazhayampallil
FRC #1155 (Bronx Science Sciborgs)
Team Role: Leadership
 
Join Date: Dec 2004
Rookie Year: 2005
Location: bronx, new york
Posts: 62
JoelP is a jewel in the roughJoelP is a jewel in the roughJoelP is a jewel in the rough
Send a message via AIM to JoelP
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

int bob=2;

void hi(void)
{
int bill=0;
bill=bob+2;
}

Last edited by JoelP : 02-03-2005 at 19:30.
  #6   Spotlight this post!  
Unread 02-03-2005, 19:38
ace123's Avatar
ace123 ace123 is offline
Registered User
AKA: Patrick Horn
FRC #0008 (Paly Robotics - http://robotics.paly.net/)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Palo Alto, CA
Posts: 50
ace123 has a spectacular aura aboutace123 has a spectacular aura about
Send a message via AIM to ace123
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"
...
...
/* Only declare and initialize global variables *once*.*/
int x=0;
int y=0;
void func1(void) {
    x=1;
}
void func2(void) {
    y=x;
}
void Default_Routine(void) {
    /* caution: do not declare the global variables (or variables with the same name) inside a function again. This may cause strange behaviour if done by accident, and will not produce an error or even a warning in most cases.*/
    // int x; <-- NO COMPILE ERROR! 
    pwm01=y+127;
    pwm02=x+127;
}
user_routines.h
Code:
...
...
/* The extern allows *global variables* declared in one file to be usable in any other file. */
extern int x;
extern int y;
void func1(void);
void func2(void);
void Default_Routine(void);
__________________
-Patrick Horn, Paly Robotics

Check out the space simulator called Vega Strike, modelled after the space simulator games Elite and Wing Commander. It's Open Source too!
If you have ever played Wing Commander, or especially Privateer, and had a feeling of nostalga derived from the you will enjoy these two Vega Strike mods: Privateer Gemini Gold and Privateer Remake!
I'm working on adding multiplayer support this year...
  #7   Spotlight this post!  
Unread 02-03-2005, 19:59
amateurrobotguy's Avatar
amateurrobotguy amateurrobotguy is offline
Lead Programmer/Senior Engineer
no team
 
Join Date: Feb 2005
Rookie Year: 2000
Location: ****
Posts: 136
amateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these partsamateurrobotguy is infamous around these parts
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()
__________________
I quit FRC over 2 years ago (more if you're reading this past 2010).
  #8   Spotlight this post!  
Unread 03-03-2005, 07:24
ldeffenb ldeffenb is offline
Registered User
AKA: Lynn Deffenbaugh (Mr)
None #0386 (Team Voltage 386)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2003
Location: Florida
Posts: 75
ldeffenb has a spectacular aura aboutldeffenb has a spectacular aura aboutldeffenb has a spectacular aura about
Send a message via ICQ to ldeffenb Send a message via AIM to ldeffenb Send a message via Yahoo to ldeffenb
Re: Static Variables

Quote:
Originally Posted by amateurrobotguy
Does C have the exception that it will run everything outside of functions once and then goto rbc only?
C is not C++, variable declarations don't "run". They are dealt with by the compiler and the linker but not the runtime. The only thing that executes is the function "main" and anything that it directly invokes.

Lynn (D) - Team Voltage Software
  #9   Spotlight this post!  
Unread 04-03-2005, 00:40
ace123's Avatar
ace123 ace123 is offline
Registered User
AKA: Patrick Horn
FRC #0008 (Paly Robotics - http://robotics.paly.net/)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Palo Alto, CA
Posts: 50
ace123 has a spectacular aura aboutace123 has a spectacular aura about
Send a message via AIM to ace123
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.14159265358979
int my_circumference=2*M_PI
This code is executed, and I think that's what the ".cinit" seciton in your map file is for.

But, 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.
__________________
-Patrick Horn, Paly Robotics

Check out the space simulator called Vega Strike, modelled after the space simulator games Elite and Wing Commander. It's Open Source too!
If you have ever played Wing Commander, or especially Privateer, and had a feeling of nostalga derived from the you will enjoy these two Vega Strike mods: Privateer Gemini Gold and Privateer Remake!
I'm working on adding multiplayer support this year...
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
Need help with Variables ? David Bryan Programming 5 23-02-2004 07:42
Joystick Variables fred Programming 8 20-02-2004 10:58
Simple problem with variables sear_yoda Programming 4 05-02-2004 09:12
VB Program to monitor robot variables DanL Programming 7 15-02-2002 22:35


All times are GMT -5. The time now is 03:00.

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