"Steven Woody" <na********@gmail.com> writes:
i get a struct as below,
typedef struct
{
uint16_t id;
long offset;
} foo_t;
foo_t foo;
somewhere in the code, i need the address of the 'id' member, so i get
it using "& foo.id". this has no problem with GNU C compiler, but with
another compile (IAR C), i got an warning,
"warning use of address of unaligned structure member"
so i want to ask, why using address of an unaligned structure memeber
was considered as a fault. and how do i elimited it?
Show us a complete compilable program that illustrates the problem.
Be sure to include either the definition of uint16_t, or a #include
for the header that defines it (<stdint.h> if you're using a C99
implementation or a C90 implementation that provides it as an
extension). Better yet, change the declaration of id to unsigned int
(you said int is 16 bits on your implementation).
For example, this program:
#include <stdio.h>
#include <stddef.h>
int main(void)
{
typedef struct {
unsigned int id;
long offset;
} foo_t;
foo_t foo;
unsigned int *ptr = &foo.id;
printf("sizeof(unsigned int) = %d\n", (int)sizeof(unsigned int));
printf("sizeof(long) = %d\n", (int)sizeof(long));
printf("sizeof(foo_t) = %d\n", (int)sizeof(foo_t));
printf("offsetof(foo_t, id) = %d\n", (int)offsetof(foo_t, id));
printf("offsetof(foo_t, offset) = %d\n", (int)offsetof(foo_t, offset));
return 0;
}
*might* trigger the warning. If it does, show us the exact diagnostic
from the compiler and the output of the program. You can ignore any
warning about ptr being unused. (If offsetof(foo_t, id) is anything
other than 0, your implementation is badly broken.)
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.