Advertisement

Quick Question. Global Vars...

Started by June 24, 2002 10:20 AM
12 comments, last by Radagar 22 years, 5 months ago
How do you declare a Global Var inside a function in C++? Is it possible? Normally global vars are declared outside of functions, but can you declare a var inside a function and access it outside functions? Thanks! ~~~~~~~~~~~ Chris Vogel ~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Somebody correct if I''m wrong, but I don''t think you can do that. Declaring it outside the function is what makes it global!
Advertisement
You can''t.

**************************
A year spent in artificial intelligence is enough to make one believe in God.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
What are you trying to achieve by declaring a global in a function? Or are you just curious?
How about:

int* myVarRef = NULL;int func1(void){  static int myVar = 1;  myVarRef = &myVar} 


This should work. Not very recommended though. You can''t reference it by name though.
I got it now... I created the global var i needed and just returned the value from the function into the global var.. I''m still pretty new to C, but I''m slowly getting it.

What still throws me WAY off is pointers. I just don''t really follow them. And then there are double pointers.. I wish I could find a good explanation of what pointers are for. I know WHAT they are and some of what they do. I just don''t understand WHEN to use them. And when I see them in code that I didn''t write, I have trouble following it.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
Advertisement
quote: Original post by Radagar
I got it now... I created the global var i needed and just returned the value from the function into the global var.. I''m still pretty new to C, but I''m slowly getting it.

What still throws me WAY off is pointers. I just don''t really follow them. And then there are double pointers.. I wish I could find a good explanation of what pointers are for. I know WHAT they are and some of what they do. I just don''t understand WHEN to use them. And when I see them in code that I didn''t write, I have trouble following it.



~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~


Just keep on going. It takes some getting-used-to. Here is a very good introduction to pointers:

http://www.iota-six.co.uk/c/24_point.htm

ENJOY



**************************
A year spent in artificial intelligence is enough to make one believe in God.
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog
I''m just starting to understand pointers (I think) and one of the best uses that someone pointed out is say you want a pass a class to a function... why pass the entire class which might be HUGE, when you can simply pass an address to that class? Don''t remember who said that, but it has helped me a lot recently
Peon
quote: Original post by Radagar
What still throws me WAY off is pointers.


Hang in there. After a while that little light will switch on (you know, that very instance it all of sudden makes sense. My first computer science teacher called this, "getting the aha!" Very accurate description IMO ) and after that, they''ll become second nature.

Just remember this mantra:

"Pointers are good. Pointers are good..."


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
quote: Original post by JuNC
How about:

int* myVarRef = NULL;int func1(void){  static int myVar = 1;  myVarRef = &myVar} 



Yikes! Don''t do that! Although it should compile fine, it will not perform correctly. Local variables are created at the start of a function, and destroyed when the function is done. If you assign a pointer to a local variable (which will soon be destroyed) you are left with a dangling pointer (memory leak). The data the pointer points at will become irrelevant and useless.

Something that might help you with pointers:


  int iNumber = 10;int* pPointerToNumber = &iNumber  


iNumber is an integer, a plain old number. You already know this.
pPointerToNumber is a pointer (denoted by the * after the int). pPointerToNumber stores the address of iNumber. When you use pPointerToNumber, you are requesting whatever data exists at the address that has been assigned to pPointerToNumber. In the example, pPointerToNumber is assigned to the address of iNumber (&iNumber), but it could be reassigned at any time during runtime.

Try to think of it this way:

A data variable is a house.
A pointer is the house''s street address.

Hope that helps some.
-Mike

This topic is closed to new replies.

Advertisement