Problem when trying to assign a value into a member of a typedef stuct variable
I have declared a typedef structure like that:
typedef struct {
u8 member_a;
u32 member_d;
u32 member_c;
u16 member_d;
} __attribute__(开发者_开发技巧(packed)) fourmembers;
Then, I have created a variable named "limp" which is of type "fourmembers":
fourmembers limp;
Following, I've tried to assign a value to "member_a" member of the "fourmembers" variable like that:
limp.member_a = 0x20;
the result is that GCC gave the following error:
error: 'fourmembers' has no member named 'member_a'
Could anyone please advise me on what am I doing wrong?
You have two member variables called member_d
. That's probably not helping matters.
Once I alter that, I can get a short code snippet to compile without problems. So if this doesn't fix your problem, you'll need to post a small, complete example that demonstrates the issue.
What you're doing wrong is not looking at the first compiler error - the one that tells you why the compiler couldn't create the fourmembers
struct. This error would be that u8
doesn't exist.
Hmm, since you asked for advice on what you're doing wrong:
- Using nonstandard names for fixed-size types instead of the standard names
uint8_t
uint16_t
etc. This is actually the source of your compile error. - Using
__attribute__((packed))
. Forget you ever learned about it. It's always wrong. - Not giving us a sufficiently complete code fragment to determine the cause of the error.
精彩评论