dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Oct 16, 1998

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


The Scope of A Local Loop Counter

C++ allows you to declare variables wherever you need them, thus enabling immediate initializations. A good example is a loop counter. It can (and should) be declared inside a for-statement:
 
void f() 
{
for (int i = 0; i < 10; i++) // i declared and initialized inside a for-statement
{
		cout << i <<endl; //output 0 to 9
}
int *p = i; //compilation error, i no longer exists. It was allowed in earlier stages
}
In earlier stages of C++, a local variable declared this way remained accessible in its enclosing block. This was a source for bugs and name hiding. Consequently, the standard has been revised to fix this loophole, so to speak, and local variables created this way are inaccessible outside the for-statement in which they were declared. In the example above, i is inaccessible outside the loop.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date