Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Radagar

Quick Question. Global Vars...

This topic is 5930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ~~~~~~~~~~~

Share this post


Link to post
Share on other sites
Advertisement
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!

Share this post


Link to post
Share on other sites
You can''t.

**************************
A year spent in artificial intelligence is enough to make one believe in God.

Share this post


Link to post
Share on other sites
What are you trying to achieve by declaring a global in a function? Or are you just curious?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
~~~~~~~~~~~

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 */
/*=========================================*/

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!