Can you please tell me the difference between these
Code:typedef struct aStructure
{
int a;
} object;
Code:typedef struct
{
int a;
} aStructure;
I am a bit confused..Code:struct aStructure
{
int a;
} obj;
Printable View
Can you please tell me the difference between these
Code:typedef struct aStructure
{
int a;
} object;
Code:typedef struct
{
int a;
} aStructure;
I am a bit confused..Code:struct aStructure
{
int a;
} obj;
The first one is a typedef'd struct; the type is "object", and it is defined as a struct aStructure. The second one is the same, except the struct is anonymous, because it does not need a name if it is always going to used via the typedef (in this case "aStructure"). A common situation where the struct does need a name is if it requires an internal pointer to itself, eg:
Linked lists work this way; the issue is you cannot use the typedef name ("object *p") inside the definition unless the definition follows the declaration:Code:typedef struct aStructure
{
int a;
struct aStructure *p;
} object;
In which case you need a named struct anyway.Code:struct astruct;
typedef struct astruct object;
struct eg {
object *p;
};
The third one is a struct definition which includes an instantiation, "obj". Ie, obj is a variable of type struct astructure. This is a little confusing because the first two examples, with the typedefs, use the same notation to name the typedef.