-
Typecasting question
Greetings all,
In the code below, can someone explain the char ** line?
I think it assigns the value 0 to an astructure[foo] array element. I just don't understand why char in (char **) is used. It is causing gcc to barf up a "dereferencing type-punned pointer will break strict aliasing rules" warning. I could type-cast it again to void, however I dont know why it was casted to char to begin with. The segment of code is part of a huge project I am working on, that is quite established.
Code:
struct mystruct
{
u_int16_t foo;
u_int16_t bar;
};
struct mystruct *astructure;
(*(char **)&astructure)[foo] =0;
-
It sets the foo'th character of the pointer named astructure to zero.
The [foo] on that line is unrelated to the member named foo in struct mystruct - presumably the name of another variable.
I suspect it is a hack that was found to achieve some desirable effect on some target machine. Subsequent and preceding usage of astructure might give some clue about what the purpose and desired effect is.
I further suspect the desired effect - whatever it is - is only achieved on a machine with a particular endianness (eg it might work on a little endian machine, but not on a big endian or middle endian machine).
-
Thanks
Grumpy,
Thanks for the reply. Your assumptions were almost entirely correct. The source was written with a specific machine in mind, and included conversion utilities for endianness, (htons(), htonl(), ntohs(), ntohl())
What was confusing me was the member variable and pointer foo. Poor naming scheme, and/or a lack of attention to detail on my part.
Cheers,
Hoksila