
Help with structure element casting required
Quote:
> #include <unistd.h>
For printf(), you should include <stdio.h>.
Quote:
> struct calling_party_byte1_struct {
IMHO, "_struct" at the end of the name is useless as C already
requires the "struct" keyword in front of the name. Typedefs and
C++ are a different matter.
Quote:
> unsigned char info_elem_id : 7;
> unsigned char filler1 : 1;
Filler bit-fields can be left unnamed.
Quote:
> };
[other structures elided -- are these from GSM, by the way?]
Quote:
> printf("CALLER: PI %d", (struct calling_party_byte4_struct
> *)(cgp.uval4).presentation_indicator);
This is only three steps from a solution. First, the dot
operator has higher precedence than the cast, so you need to take
the cast inside the parentheses:
printf("CALLER: PI %d",
((struct calling_party_byte4_struct *) cgp.uval4)
.presentation_indicator);
Second, this is casting a structure to a pointer which C does not
allow. If you want a different-typed pointer pointing to the
structure, you need to cast the address of the structure:
printf("CALLER: PI %d",
((struct calling_party_byte4_struct *) &cgp.uval4)
.presentation_indicator);
Third, the outer dot operator expects to get a structure on its
left side but is given the address of one instead. Change it to
an arrow:
printf("CALLER: PI %d",
((struct calling_party_byte4_struct *) &cgp.uval4)
->presentation_indicator);
Then it will work.
Finally, note that uval4 is already an union so the cast isn't
actually needed:
printf("CALLER: PI %d",
cgp.uval4.cgp4struct.presentation_indicator);
--