C: Global Variables versus Parameters
NelsonMandella Wrote:Say you have a source file called main_menu, and in that file you have a global variable, g_is_main_menu_open, obviously functions outside of the source will want to query it so it's helpful to create an accessor function like this:
BOOL is_main_menu_open() {
return g_is_main_menu_open;
}
When you iterate through a list of game entities(and eventually you will), you may want want to use pointer parameters, rather than passing indices, it's quicker, more elegant, and makes your code easier to read.
...
This is where it gets subjective and takes experience, and is kind of the "art" of C programming. Sometimes I prefer to use an accessor to make things cleaner, but sometimes I just go bare-bones and access the global directly from the other file to make things cleaner. One thing I will say that I've learned about C over the years: try to listen carefully to what everybody says and then disregard them and do whatever you want. Rebellion is liberating!
| Messages In This Thread |
|
C: Global Variables versus Parameters - Lizard Man - Jan 12, 2010, 08:20 PM
C: Global Variables versus Parameters - AnotherJake - Jan 12, 2010, 08:50 PM
C: Global Variables versus Parameters - AnotherJake - Jan 12, 2010, 09:04 PM
C: Global Variables versus Parameters - NelsonMandella - Jan 12, 2010, 10:46 PM
C: Global Variables versus Parameters - AnotherJake - Jan 12, 2010 10:57 PM
C: Global Variables versus Parameters - DoG - Jan 13, 2010, 05:03 AM
C: Global Variables versus Parameters - Skorche - Jan 13, 2010, 08:19 AM
C: Global Variables versus Parameters - NelsonMandella - Jan 13, 2010, 11:22 AM
C: Global Variables versus Parameters - AnotherJake - Jan 13, 2010, 11:41 AM
C: Global Variables versus Parameters - Najdorf - Jan 13, 2010, 01:14 PM
C: Global Variables versus Parameters - Lizard Man - Jan 13, 2010, 08:22 PM
|
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Accessing an inherited class's variables | Tobs_ | 22 | 8,372 |
Feb 28, 2007 05:26 PM Last Post: mac_girl |
|
| Problems with variables in Obj-C | vnvrymdreglage | 16 | 5,920 |
Oct 2, 2006 10:19 PM Last Post: vnvrymdreglage |
|
| Should global variables be pointers or full objects? | ia3n_g | 1 | 2,066 |
Aug 4, 2006 05:53 PM Last Post: OneSadCookie |
|
| where do global variables fall into the memory type? | WhatMeWorry | 3 | 2,523 |
Jun 5, 2006 02:45 PM Last Post: OneSadCookie |
|
| Arrays or variables containing executable functions | Jones | 4 | 3,732 |
Jun 2, 2006 08:35 AM Last Post: Zekaric |
|

