Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 21st, 2006, 10:21 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!

Reply With Quote
  #2 (permalink)  
Old October 22nd, 2006, 03:31 AM
Registered User
 
Join Date: Oct 2006
Location: Izhevsk, , Russia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to woldemar
Default

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.

Reply With Quote
  #3 (permalink)  
Old October 23rd, 2006, 03:57 PM
Authorized User
 
Join Date: Mar 2006
Location: Tucson, AZ, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

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.







Reply With Quote
  #4 (permalink)  
Old October 23rd, 2006, 05:24 PM
Authorized User
 
Join Date: Mar 2006
Location: Tucson, AZ, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

Let me know if this help:).

Reply With Quote
  #5 (permalink)  
Old October 24th, 2006, 06:53 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

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
Reply With Quote
  #6 (permalink)  
Old October 25th, 2006, 06:29 PM
Authorized User
 
Join Date: Mar 2006
Location: Tucson, AZ, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

Good *point; :)

Reply With Quote
  #7 (permalink)  
Old October 25th, 2006, 06:47 PM
Authorized User
 
Join Date: Mar 2006
Location: Tucson, AZ, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

But remember that he is a begginer. If he does't know about global variables the pointer will turn to difficult for him.

Reply With Quote
  #8 (permalink)  
Old October 26th, 2006, 05:47 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

That's true but at an early stage he still shouoldn't think that global variables are okay that sprouts into bad habits

 ~ Geo
Reply With Quote
  #9 (permalink)  
Old October 26th, 2006, 05:48 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

*point = abovePost;

 ~ Geo
Reply With Quote
  #10 (permalink)  
Old October 26th, 2006, 10:35 AM
Authorized User
 
Join Date: Mar 2006
Location: Tucson, AZ, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to skoob1152 Send a message via Yahoo to skoob1152
Default

*point2 = abovePost[10];

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable scope in XSLT? trufla XSLT 3 October 24th, 2008 01:45 PM
Scope Issue iceman90289 C# 2005 8 April 5th, 2008 03:41 PM
Variable with application scope madhukp PHP How-To 0 January 8th, 2005 12:41 AM
Scope of array jakeone Beginning PHP 1 November 4th, 2003 08:10 PM
Error: Intrinsic variable with Application scope danielh Classic ASP Basics 1 June 22nd, 2003 03:24 PM



All times are GMT -4. The time now is 06:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.