S
Stephen J. Fromm
What are the scoping rules for a for loop?
Example:
int i;
for(i = 0; i < 10; i++) {
int j;
/* more code here */
}
In this example, is the scope (a) "renewed" upon each iteration of
loop, or (b)common across all iterations? (My impression from
compiling code is "(a)").
This is in reference to scoping within blocks: one can write, absent
any control structure,
{
int j = 7;
/* more code */
}
and the lifetime of j is confined to the { }s, if I recall correctly.
With this in mind, (a) is like
int i = 0;
{
int j;
/* more code */
}
/* test i */
{
int j; /* New "instance" of j */
/* more code (again) */
}
....
whereas (b) is like
int i = 0;
{
int j; /* only 1 instance of j */
/* more code */
/* test i */
/* more code */
}
TIA,
sjfromm
Example:
int i;
for(i = 0; i < 10; i++) {
int j;
/* more code here */
}
In this example, is the scope (a) "renewed" upon each iteration of
loop, or (b)common across all iterations? (My impression from
compiling code is "(a)").
This is in reference to scoping within blocks: one can write, absent
any control structure,
{
int j = 7;
/* more code */
}
and the lifetime of j is confined to the { }s, if I recall correctly.
With this in mind, (a) is like
int i = 0;
{
int j;
/* more code */
}
/* test i */
{
int j; /* New "instance" of j */
/* more code (again) */
}
....
whereas (b) is like
int i = 0;
{
int j; /* only 1 instance of j */
/* more code */
/* test i */
/* more code */
}
TIA,
sjfromm