This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: diagnostic request
- From: Carlo Wood <carlo at alinoe dot com>
- To: phr-2002 at nightsong dot com
- Cc: bug-gcc at gnu dot org
- Date: Wed, 6 Feb 2002 16:53:04 +0100
- Subject: Re: diagnostic request
- References: <20020206111854.2994.qmail@brouhaha.com>
On Wed, Feb 06, 2002 at 11:18:54AM -0000, phr-2002@nightsong.com wrote:
> int a[3] = {1, 2, 3};
> test() { a[a[0]]=1; }
>
> looks like it sets a[1] to 1. However, according to some intricate
> reading of the ANSI C standard, its behavior is apparently undefined--
I don't see what is undefined about this particular example.
Perhaps you mean:
for(int i = 1; i <= 2; ++i)
a[a[0] - 1] = i + 1;
This would become
for(int i = 1; i <= 2; ++i)
{
int a0 = a[0];
a[a0 - 1] = i + 1; // Garanteed/assumed not to change a[0] (which is the 'problem').
}
and thus become:
int a0 = a[0];
for(int i = 1; i <= 2; ++i)
a[a0 - 1] = i + 1;
[
and finally
int a0 = a[0];
a[a0 - 1] = 3;
which is equivalent to
a[0] = 3;
instead of the 'expected'
a[0] = 2;
a[1] = 3;
]
--
Carlo Wood <carlo@alinoe.com>