What's the difference between:
typedef struct part
{
int a;
} Part;
and
typedef struct
{
int a;
} Part;
I know the second one is "anonymous" but are they different?
Structs are value types and can be used to create objects that behave like built-in types. Structs share many features with classes but with the following limitations as compared to classes. Struct cannot have a default constructor (a constructor without parameters) or a destructor.
-> is a shorthand for (*x). field , where x is a pointer to a variable of type struct account , and field is a field in the struct, such as account_number . Show activity on this post.
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.
Generally speaking, objects bring the full object oriented functionality (methods, data, virtual functions, inheritance, etc, etc) whereas structs are just organized memory. Structs may or may not have support for methods / functions, but they generally won't support inheritance and other full OOP features.
In addition to Mike Seymour answer, here's an example of why:
Linked list for example
// this works
typedef struct part {
struct part *next;
} Part;
// this doesn't work
typedef struct {
struct Part *next;
} Part;
// neither this
typedef struct {
Part *next;
} Part;
In both cases you define a structure type, and a type alias called Part
which refers to that type.
In the first case, you also define a structure name part
, so the structure type can also be referred to as struct part
, or just part
in C++.
In the second case, the structure itself has no name (hence "anonymous"), and can only be referred to by the type alias. Since that alias is only declared after the structure definition, the structure cannot refer to itself.
And in addition to @EoiFirst 's answer,
typedef struct part
{
int a;
struct part *next;
} Part;
This is valid because next
is a pointer to struct part
, so it has the size of a pointer-type.
C is a typed language, so the compiler knows the type of next
, and after struct part
is fully defined, it will be able to determine that the code below is valid. It knows that next
is a pointer to struct part
, that has a field int a
.
void test(struct part *p) {
if (p != NULL && p->next != NULL) {
printf("%d\n", p->next->a); // this is valid
}
}
You could declare
typedef struct part
{
int a;
void *next;
} Part;
The size of struct part
would still be the same as the declared above. Now next
is a pointer to anything, and the code with test()
above would compile with error.
warning: dereferencing 'void *' pointer
error: request for member 'a' in something not a structure or union
You would need to write printf("%d\n", ((struct part *) p->next)->a);
Also, you will not be able to declare this,
typedef struct part
{
int a;
struct part nested;
} Part;
You will get a compiler error.
error: field 'nested' has incomplete type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With