Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
–
–
Personally I would switch off the warning for this section of code using pragmas like in the example below.
char *g(void)
#pragma diag_suppress=Pa039
return (char *)(&d.b);
#pragma diag_default=Pa039
If this is not an option, it is possible to find the offset of b
in my_t
and add this to the address of d
char *f(void)
return (char *)&d + offsetof(my_t, b);
–
–
–
–
Because a double
is typically larger than an int
, and because the struct is packed, the field b
is not properly aligned when the smaller field appears first.
Put the larger field first and both should be properly aligned:
typedef struct {
double b;
int a;
} my_t;
–
–
Firstly: you should be sure that double variable aligned inside data type by 4 bytes, your code is correct!
#pragma pack(push, 4)
typedef struct {
int a;
double b;
} my_t;
#pragma pack(pop)
Secondly: your variable address should be located corresponded
#pragma data_alignment = 4
my_t d;
char * p = (char *)(&(d.b));
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.