Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #106818
| Newsgroups | comp.lang.c |
|---|---|
| Date | 2017-03-23 10:23 -0700 |
| References | <ob0drl$agb$1@dont-email.me> |
| Message-ID | <828e6fda-e420-4060-9339-0e699adfffd0@googlegroups.com> (permalink) |
| Subject | Re: for-loop scopes |
| From | "Rick C. Hodgin" <rick.c.hodgin@gmail.com> |
On Thursday, March 23, 2017 at 8:11:26 AM UTC-4, Bart wrote:
> This line in the 'struct padding' thread:
>
> for(long i = 0; i<N; i++)
>
> prompted me to do more tests:
>
> {
> double i; // scope 1
> for (int i=0; i<10; ++i) // scope 2
> i; // scope 2
> i; // scope 1, deliberate misalignment
>
> Is is correct that the 'int i' inside the for-loop header creates a new
> scope for the following single statement, even without the existence of
> a formal {...} block?
>
> I'm just asking because I'm finding it hard to believe that those last
> two final 'i' terms switch types with merely an intervening ';'.
As Ike Naar points out, it doesn't switch types. There are two
isolated scopes, and the names referenced within result in access
to two completely isolated variables:
Local variable scope:
| +------------+
+===>| double | i |
| +------------+
|
+===>For-loop inner scope:
| +------------+
+===>| int | i |
+------------+
And if you had a second inner loop:
Local variable scope:
| +------------+
+===>| double | i |
| +------------+
|
+===>For-loop inner scope:
| +------------+
+===>| int | i |
| +------------+
|
+===>For-loop inner-inner scope:
| +------------+
+===>| int | i |
+------------+
... and so on ...
When your "i" variable is referenced in code, depending on what
scope you're in at the time, the compiler will search upward in
the hierarchy of scopes and find the one that matches. If it
doesn't find the name in its scope, go up one. If it's not found
there, go up one. And repeat until all scopes are exhausted or
it's found.
Each inner scope effectively hides access to the variables of the
same name in higher scopes. So, not only are tokens identified by
their name, but also by their locality.
-----
In your compiler, you surely have mechanisms to perform this kind
of search up the chain of scopes to find the suitable match? I
was very surprised by your question.
Thank you,
Rick C. Hodgin
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 12:11 +0000
Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 14:14 +0100
Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 13:45 +0000
Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 15:48 +0100
Re: for-loop scopes Robert Wessel <robertwessel2@yahoo.com> - 2017-03-23 23:01 -0500
Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-24 10:27 +0000
Re: for-loop scopes jameskuyper@verizon.net - 2017-03-24 10:43 -0700
Re: for-loop scopes Robert Wessel <robertwessel2@yahoo.com> - 2017-03-26 21:57 -0500
Re: for-loop scopes Keith Thompson <kst-u@mib.org> - 2017-03-23 08:37 -0700
Re: for-loop scopes Ike Naar <ike@iceland.freeshell.org> - 2017-03-23 16:31 +0000
Re: for-loop scopes "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-03-23 10:23 -0700
Re: for-loop scopes BartC <bc@freeuk.com> - 2017-03-23 18:07 +0000
Re: for-loop scopes David Brown <david.brown@hesbynett.no> - 2017-03-23 20:05 +0100
Re: for-loop scopes jameskuyper@verizon.net - 2017-03-23 12:33 -0700
Re: for-loop scopes John Bode <jfbode1029@gmail.com> - 2017-03-24 11:35 -0700
Re: for-loop scopes Noob <root@127.0.0.1> - 2017-03-23 21:06 +0100
Re: for-loop scopes Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-03-28 19:15 -0700
Re: for-loop scopes Andrey Tarasevich <andreytarasevich@hotmail.com> - 2017-03-28 19:19 -0700
Re: for-loop scopes supercat@casperkitty.com - 2017-03-29 13:14 -0700
csiph-web