Thread: varaible trouble

  1. #1
    Unregistered
    Guest

    varaible trouble

    i have got multiple .c files, and i wish to use the same variable throughout the files. "i" the variable, is used as a counter of members in the array. how do i delclare or initialise this variable so that i can use it in all of the .c files? when i delcare the varibale in each file i get erros saying that it has allready been declared in the other files, and, wheni dont declare it, i get errors messages tellgn me i need to declare it. any ideas people? cheers.

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Declare i as a global variable in one of your modules and then extern it to the other modules.

    In main.c:
    Code:
    int i;
    In remaining .c files:
    Code:
    extern int i;
    Jason Deckard

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In one file, and only one file declare your variable as you normally would. In all other files, add the word "extern" in front of your variable. As an example:

    File1:

    #include ?????
    #include ?????

    int i;

    int main()
    {
    }


    File2:

    #include ?????

    extern int i;

    function1()
    {
    }

    function2()
    {
    }


    File3:

    #include ?????
    #include ?????
    #include ?????

    extern int i;

    function3()
    {
    }

    function4()
    {
    }

    function 5()
    {
    }


    This should give you a hint as to how to proceed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Lightbulb external

    I think this should work:
    Globally in the main file:
    int iSomething = 0;

    In all files which should share this instance:
    extern int iSomething;

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I'm just too fast for you guys
    Jason Deckard

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Cool Yeah..

    You get a quick response on this board....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM