Accessing a variable in several functions in several files

This is a discussion on Accessing a variable in several functions in several files within the C Programming forums, part of the General Programming Boards category; I am in the process of seperating my project into multiple files. I have a variable(my only truely global variable) ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    202

    Accessing a variable in several functions in several files

    I am in the process of seperating my project into multiple files. I have a variable(my only truely global variable) that needs to be set in a dialog box(yes this is a windows app but its a C question) which I can do and then it needs to be read by the AI in a seperate file. I tried declaring it in a header file they both accessed but then they both accessed it but it seemed like the AI was getting a calue that wasn't what was selected in the Diallog(It was coming up like it was never initilized). Then I tried a hackish message of have the Dialog return the value I selected and then passed that to the AI function as a parameter but that led to it always be 1. Im sure Im not the first with this problem but I can't think off anyway to have it work which I haven't tried. So how can I acess a variable in multiple files?

    Ray Koons
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /*file1.c*/
    int myvar;
    
    int main(void)
    {
        ...stuff...
        return 0;
    }
    /*end of file1.c*/
    
    /*file2.c*/
    extern int myvar;
    
    ...stuff...
    /*end of file2.c*/
    
    /*fileN.c*/
    extern int myvar;
    
    ...stuff...
    /*end of fileN.c*/
    There ya go.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Thank you very much. That worked! :-)
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions with variable argument count..
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2009, 09:18 AM
  2. Replies: 12
    Last Post: 02-12-2009, 01:39 PM
  3. Replies: 7
    Last Post: 02-01-2009, 03:53 PM
  4. Replies: 4
    Last Post: 06-11-2004, 06:18 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21