S
Skarmander
pete said:Skarmander said:Mark said:http://www.faqs.org/faqs/C-faq/abridged/
3.1: Why doesn't the code "a = i++;" work?
A: The variable i is both referenced and modified in the same
expression.
Here is the code that I have written:
#include <stdio.h>
int main ()
{
int a[] = {1,2,3,4,5,6};
int i=1;
int j;
a = i++;
for(j=0;j<6;j++)
printf("%d\n",a[j]);
}
Outout (using gcc (GCC) 3.4.2 (mingw-special))
1
1
3
4
5
6
It works fine for me !
WHAT??? Take another look at your results...
HINT: where is the number 2???
I ate it.
Reread the program, then look for a brown paper bag to pull over your
head. HINT: what does assignment do?
Exactly.
Is assignment, a sequence point?
No!
Is an expression defined when it modifies
and accesses an object without an intervening sequence point?
No!
Is a = i++; C code?
No,
it's just gibberish that looks like C code.
Yes, I know. I did read the rest of the thread.
The comment "where is the number 2" still makes no sense. If you did
*not* expect this to be undefined behavior (as the OP did) then the
result he got indeed "work fine for him". That is, a = i++ sets a[1]
to 1 and there is no surprise.
I might as well have posted:
WHAT??? Take another look at your results...
HINT: where are the demons that are supposed to fly out of your nose???
S.