Originally Posted by
shaswat
Hi All,
I was wondering that why this snippet of code gives error
Code:
struct{
u8 Address1;
u8 Address2;
}st_varib;
...
If you're not using typedef, you probably want to be using this form instead, judging from the rest of your code:
Code:
struct st_varib {
u8 Address1;
u8 Address2;
};
This declares struct st_varib as a type. Remember that struct tags have a separate name space, so the following is a valid C statement which declares a symbol called st_varib whose type is struct st_varib:
Code:
struct st_varib st_varib;
Some programmers find that confusing, so they declare a typedef like this:
Code:
struct st_varib {
u8 Address1;
u8 Address2;
};
typedef struct st_varib st_varib;
After writing that, you can now say 'st_varib s;' to declare that s has type 'st_varib' (which is the same as type 'struct st_varib').
Some programmers find the above confusing because 'st_varib' appears twice with different meaning, so they write this equivalent definition instead:
Code:
struct _st_varib {
u8 Address1;
u8 Address2;
};
typedef struct _st_varib st_varib;
This version creates a throwaway symbol '_st_varib' in the struct tag name space which the programmer intends to use exactly one time (in the typedef line). From then on he uses 'st_varib' as the type name. Finally the above can be simplified to the following (which you used in your example):
Code:
typedef struct {
u8 Address1;
u8 Address2;
} st_varib;
This version declares the struct as an anonymous struct but then typedef's it to the name st_varib. So again you can write 'st_varib s;' to declare a symbol called s which has the type 'st_varib' which is now the same as the struct type. It is conceptually equivalent to writing
Code:
struct { u8 Address1; u8 Address2; } s;
except that writing the above will probably not work very well, because each time you write struct with a member definitions list, it introduces a new type, even if the struct member definitions are exactly the same as a previous definition.