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?
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...
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.
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...
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.
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...
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.
The extern worked.
thanks,
as.
Everything is relative...