473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

scope of for loop in VC6

When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?

--
Nick H
Jul 22 '05 #1
7 2796
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?


It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.

- Pete
Jul 22 '05 #2

"Pete C." <x@x.x> wrote in message
news:D9****************@newsread1.news.pas.earthli nk.net...
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?
It's a VC6 bug.


It's not really a bug, it's a relic of pre-standard C++.
Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003 Pro edition optimizing compiler and standard library.


If you don't want to switch compilers, an alternative is this macro. Place
it at the beginning of each file.

#define for if (0); else for

Jul 22 '05 #3
Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".
By default it's turned off, which in my opinion is plain wrong.
I wish I knew a way to have it turned on by default for every new project
I start...

Walter Tross
Jul 22 '05 #4
"Walter Tross" <wa****@waltertross.com> wrote in message
news:sw***************************@40tude.net...
Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable redeclaration. I thought that the i stayed within the scope of the for loop? The same code on gcc doesn't give this error (not on the default settings at least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...
I don't know about VC6, but in VC .NET it's an option in project
properties -> C/C++ -> language. I don't know it's precise naming in
English since I have it in Italian, but it should be something like
"impose for cycle scope conformity".


Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.

Cheers
Nick H
Jul 22 '05 #5
Walter Tross wrote:

Nick Howes 2004-06-15 :
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of variable
redeclaration. I thought that the i stayed within the scope of the for loop?
The same code on gcc doesn't give this error (not on the default settings at
least). Is VC being fussy, and can I turn this fussiness off?


You are a bit off-topic here, but anyhow...

<snip>

au contraire --

This is very *much* on topic. Prior to the (pre)standard change to for-loop
scoping rules, variables declared in a for-loop were still in scope outside of
the loop.

This conformity issue, in part, is due to the very *long* time that it took for
the standard to actually be finalized/approved/ratified in the 90's.
Jul 22 '05 #6
"Nick Howes" <n.*******@warwick.ac.uk> wrote in message news:<ca**********@wisteria.csv.warwick.ac.uk>...

[ ... ]
Sorry about the OT-ness, should've looked up a visual studio newsgroup.
Thanks everyone for your advice anyhow, there's no such option in VC6 but i
will look up that VC++ Toolkit 2003 and have a look.


VC++ 6 has an option (/Za) that enforces the correct scope for
variables defined in the header of a for loop (etc.) The problem is
that when you turn this switch on, it enforces a number of other
rules, and its standard headers (among other things) won't compile
when those rules are enforced -- the result is that even though the
compiler itself has the capability, the remainder of the
implementation renders it unusable.
--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #7
Pete C. wrote:
Nick Howes wrote:
When I have a couple of for loops sequentially, and they use the same
counter name, i.e. for(int i=0; i<x; i++), Visual C++ complains of
variable redeclaration. I thought that the i stayed within the scope
of the for loop? The same code on gcc doesn't give this error (not on
the default settings at least). Is VC being fussy, and can I turn
this fussiness off?

It's a VC6 bug. Download the free VC++ Toolkit 2003 from Microsoft's site,
and use that in place of the VC6 compiler. It comes with the full VC++ 2003
Pro edition optimizing compiler and standard library.


I know we're getting OT here, but when you say "use that in place of the
VC6 compiler" do you mean that the v13 MS C++ compiler is compatible
with Visual Studio 6, i.e. interchangeable with the v12 compiler? I
wasn't aware of that; it would be very interesting to me if true.

--
Mike Smith

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
6
by: Arthur J. O'Dwyer | last post by:
I was paging through Coplien's book "Advanced C++ Programming Styles and Idioms" this afternoon and found some code that looked something like void sort(vector<foo> a) { int flip; do { for...
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
14
by: Michael Moreno | last post by:
Hello, Would you know what is best practice please between say: CODE 1: TimeSpan ts; for (i=0; i<1000; i++) {
8
by: Erik de Castro Lopo | last post by:
Hi all, Consider the following code snippet: do { int r = rand () ; } while (r != 0) ; It seems the compiler I'm using (GCC) does realise that the
9
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
2
by: ray | last post by:
Hi, all, foreach($array as $k =$v) { $foo = ...; } echo $foo; Is it allowed to access the $foo variable that is created within the loop from outside of the loop? I think it isn't allowed,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.