Thread: Global variables and functions.

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Global variables and functions.

    I have a global bariable defined, and I want one of the functions in the project to use it. I just used it in the function but I get an error 'vars' : undeclared identifier.

    Do I have to pass a global variable to a function to use it?
    Everything is relative...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, but you do have to declare the global variable higher than its first use.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Yep, they are declared at the top of the program. The function I'm trying to use it in is is a separate .c file, if I use the varriable in the file .c file, it's fine.
    Everything is relative...

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    In the separate .c file, add the line extern int vars; or whatever type it is. Anyway, declare it like you did your global variable but prepend the keyword extern to it.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    if I use extern int vars, will that create a new instance of vars? Because I want to use the other vars that is created in the main file. It's a structure and I want to use some of the entries in it.
    Everything is relative...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    No, it just tells the compiler "Hey, this is declared in another file" so the linker doesn't barf. It's the same variable so changing it in one .c file changes it in the other.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    The extern worked.

    thanks,

    as.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  4. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  5. Global Variables, include files and classes
    By sharpstones in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2005, 10:06 AM