Go Down

Topic: Noob asks WHY???? (Read 1 time) previous topic - next topic

MichaelMeissner


Forget trolls,     
Quote
If its declared globally,  isn't it declared for EVERY scope?
is this true or not?

Mostly, but there are some gotchas:

  • Declarations are processed in the order they occur in the source file.  So if you have a function that references a global variable earlier in the source file than the declaration of the global variable, the compiler will complain.

  • If you have split the code into multiple libraries, the libraries will only be able to reference the global variable if they have a declaration in scope.  Typically you would put the declaration of a global variable in a common include file (using the extern keyword), and a definition of the global variable in the library source file.

  • Declarations of the same variable in an inner scope hides the reference to the global variable for the scope of the block that the definition is in.

  • Similarly, if you use #define of the name, it hides reference to the name until you #undef the name.

Boffin1

Huha   Thanks Michael,

I think I have run foul of a couple of those gotchas along the way, and had to Mgyver my way out.

I will keep a note of these for next time.

kf2qd

Also - If you declare a global, and then inside a function declare another variable with the same name the local gets used and not the global. Have seen a couple times where a noob did that thinking it was the same variable. The local worked, but it didn't have the global value...
and then when the function exited the global value was unchanged...


Khalid

Amazed to see "WTF"...
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

Quote
Amazed to see "WTF"...
?

This must be getting on for one of the longest threads with the least OP input.

Conjecture on, dudes.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Khalid

AWOL,
The thread starter used "WTF" in his first post. I was amazed to read...
Simply...You can't afford me..

Author Of:
http://my-woodcarving.blogspot.com/
http://www.free3dscans.blogspot.com/
http://my-diysolarwind.blogspot.com/

Oops..some one gave me Karma...:)

AWOL

#21
Mar 20, 2013, 09:28 am Last Edit: Mar 20, 2013, 09:33 am by AWOL Reason: 1
I'm still puzzled by your amazement (if that's a sentence)

Quote
The thread starter used "WTF" in his first post.

S/he also used four question marks and the word "wanna", which I find almost as disappointing.
If they had spelled-out what "WTF" expands to, I'd've been upset.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up