View Single Post
  #2   Spotlight this post!  
Unread 23-01-2008, 00:50
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
Re: Global Variable Placement

Quote:
Originally Posted by robbrad View Post
I've tried to insert global variables in various places in Kevin's code only to get stack overflow, code errors (RC), sytax errors, compiler crashes and general system crashes. Where do you all put your global variables. I'm interested in global only. I'd like to minimise passing information between functions. Thanks
A good practice is to declare a variable outside of any function and then declare the variable as "extern" within the associated header file. So for example, at the top of teleop.c I declare a variable:

// this allocates memory
unsigned int foo;

Then in teleop.h I declare the variable again with the keyword extern:

// no memory is allocated, but lets the compiler know the name (foo)
// and type (unsigned int), and that it is located in another source file
extern unsigned int foo;

Then include teleop.h at the top of each source file that needs to use foo. The line will look like this:

#include "teleop.h"

-Kevin
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org