|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
October 21st, 2006, 10:21 PM
|
Registered User
|
|
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
the scope of variable
hi,
I was reading C++ Primer,4th and met a problem. In the book, it says the variable has a satement scope,which means if you define a varialbe in a statement such as in for statement, then you cannot use it out of the statement scope.but i worte a little program:
for(int i=0;i<10;i++)
{
do something;
}
cout<<i<<endl; then I complied it ,it works fine. So I cannot understand the statement scope. Can anynoe here help me? Thanks in advance!
|
October 22nd, 2006, 03:31 AM
|
Registered User
|
|
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, you may think about "for" statement as about such construction
your:
for(int i=0;i<10;i++)
{
do something;
}
is:
int i=0;
while (i<10)
{
do something;
i++;
}
Counter is not defined within statement block.
|
October 23rd, 2006, 03:57 PM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi my friend here is a better answer for you:
What is the scope for this varaible(MyVariable)?
for(int i=0;i<10;i++)
{
int MyVariable; // the scope of this variable is local because
do something; // you only have acces to the variable inside of
} // the for statement;
On the othet hand we have global variables:
#include<stream.h>
int MyGlobalVariable = 5; //This is a global variable because is
//declared before the main function
int main() // this meand that you can use this
{ // variable on any part of the code
// including in "for" statements.
return 0;
}
Ok! so the scope of variable depend where you declare the variable (local or global).
A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.
|
October 23rd, 2006, 05:24 PM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Let me know if this help:).
|
October 24th, 2006, 06:53 AM
|
Friend of Wrox
|
|
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
|
|
That was a good explanation skoob but don't forget to inform him that global variables are frowned upon due to their security. instead you should read about pointers and use local variables. Pointers are very important and save lots of time and memory if you use them correctly. unless you have constants, no variables should be declared outside of any function.
~ Geo121
|
October 25th, 2006, 06:29 PM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Good *point; :)
|
October 25th, 2006, 06:47 PM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
But remember that he is a begginer. If he does't know about global variables the pointer will turn to difficult for him.
|
October 26th, 2006, 05:47 AM
|
Friend of Wrox
|
|
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
|
|
That's true but at an early stage he still shouoldn't think that global variables are okay that sprouts into bad habits
~ Geo
|
October 26th, 2006, 05:48 AM
|
Friend of Wrox
|
|
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
|
|
*point = abovePost;
~ Geo
|
October 26th, 2006, 10:35 AM
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
*point2 = abovePost[10];
|
|
|