Welcome! Log In Create A New Profile


Header variable reference bug

Posted by owen 
Header variable reference bug
June 07, 2012 05:56PM
So I've been updating one of my 3d games in c and I notice that when every I reference a bool variable that was declared in a header file I get a crash screen. If I initialize the variable to false, everything works fine but I am wondering if I am miss using the the header files or something.

My set up is like this;

common.h has this code;
#ifndef common_h
#define common_h
bool debug;

main.c has this code;
include common.h

bool *myboolv;
void store( bool *boolv ){
 myboolv = boolv;

void check_value(){
  if( *boolv == true) { /* crashes on this if statement */ }
void main(){

common.c doesn't have anything in it. If I initialize debug in the main it works fine but other wise I not sure why I getting the error.
Re: Header variable reference bug
June 07, 2012 10:52PM
Sounds like there's more to it (check_value() is dereferencing *boolv which isn't declared globally), but you shouldn't be defining variables in header files (especially if they will be included in several .c files). You will end up getting compilation errors caused by multiple global variables with the same names.

What you want to do is declare it using the "extern" keyword in the header file, then declare it again in one (and only one) .c file without the extern keyword. That will make it accessible by all .c files that include the header, but only the .c file that contains the non-extern declaration will actually allocate memory space for it.
Re: Header variable reference bug
June 08, 2012 04:29AM
Ok, cool, I thought the ifndef was suppose to take care of the. I am working on my menu system. will test out the extern syntax and see if it solves it.
Sorry, only registered users may post in this forum.

Click here to login