Closed dumblob closed 9 years ago
(int)x
gives you a copy of x
, so it's natural that modifying it has no effect on the original variable.
Well, I don't feel it naturally - especially if considering cases with casting any
variables (in such cases, there is absolutely no assumption, that the content of the variable will be copied) or variables of the same type as the casted type.
0$ dao -e 'for (x: any = 5; x < 10; ++(int)x) {}'
keyboard interrupt...
Casting a value of primitive type yields its copy, just as with assigning or passing to a routine. It is weird to expect a reference here. It doesn't matter whether it is any
or the same type -- casting always yields an rvalue, in any sane language.
In general, copying of primitive types makes sense - it's just a bit unfortunate with the ++
operator -- yes, old C habits die hard, because C forbids this:
0$ gcc -pedantic -std=c99 del/test.c
del/test.c: In function ‘main’:
del/test.c:8:29: error: lvalue required as increment operand
printf("%d, %d, %f\n", x, ++(int)x, ++(int)x);
^
del/test.c:8:39: error: lvalue required as increment operand
printf("%d, %d, %f\n", x, ++(int)x, ++(int)x);
^
0$ gcc -pedantic -std=c99 del/test.c
del/test.c: In function ‘main’:
del/test.c:9:29: error: lvalue required as increment operand
printf("%d, %d, %f\n", x, ++((int)x), ++((int)x));
^
del/test.c:9:41: error: lvalue required as increment operand
printf("%d, %d, %f\n", x, ++((int)x), ++((int)x));
^
What about forbidding it as well (there is absolutely no sense in such construction - it's just a source of mistakes)?
Of course it is better to forbid or warn about meaningless modifications of data.
Of course it is better to forbid or warn about meaningless modifications of data.
Done.
The last two keyboard interrupts are due to infinite looping.