Arne said:
Maxim said:
Arne said:
Why does this code not compile in GCC 4.x:
int main()
{
int a[100];
void *pa = (void *)a;
Useless cast, remove it.
That does make sense. Although it is interesting, that g++ 3.3 did
not even warn about this.
GCC used to have "cast-as-lvalue" as an extension. If you compiled
with warnings on (-Wall -Wextra -ansi -pedantic), it should tell you.
No, that also does not work, one has to explicitly convert:
Explicit conversions aren't necessary when converting to (void *).
pa = (int *)((char *)pa + 2);
This is likely to have alignment problems (if int has 4-byte alignment
then it can't be aligned correctly both before and after this
operation).
These solutions rely on 'pa' being a 'void *'. If it were an int *,
then we would have undefined behaviour because int* might
be a different size/representation to char* .