Reputation: 67
Can i ask if the syntax for these two are the same? I keep getting confused because typedef construct have the struct name after the closing of the curly brace but for here it seems like the variable names are the ones defined after the closing of the curly brace.
struct{
int age;
float height;
}person1,*personPtr;
VS
struct person{
int age;
float height;
};
struct person person1;
struct person *personPtr;
Upvotes: 1
Views: 216
Reputation: 3150
Can i ask if the syntax for these two are the same?
No, they are not.
The first version declares an anonymous struct (as it has no name) and two variables using that type,
The second version names the type, and then uses the type name to declare the variables.
I keep getting confused because typedef construct have the struct name after the closing of the curly brace but for here it seems like the variable names are the ones defined after the closing of the curly brace.
Yes, variable declarations and typedef declarations are designed to be very similar, on purpose.
For example, int age;
declares a variable called age
. By adding a typedef before the declaration, as in typedef int age;
we instead made age
a type and not a variable. We can then use it to declare a variable
age my_age;
same as
int my_age;
And it works the same for structs
struct
{
int age;
float height;
} person;
declares a variable person
, while
typedef struct
{
int age;
float height;
} person;
says that person
is a type.
The design of making a typedef declaration look very similar to a variable declaration might have seemed like a good idea at the time, but perhaps it was not. More confusing than brilliant?
Upvotes: 0
Reputation: 23218
Other answer addresses the question: "if the syntax for these two are the same?". Here, I will focus on the following statement:
"I keep getting confused because typedef construct have the struct name after the closing of the curly brace but for here it seems like the variable names are the ones defined..."
So, although your post does not actually include a snippet showing a typedef struct {
, I will include one here to help identify the differences between two cases, the unnamed struct
and a typedef struct
.
The difference between a typedef, for example...
typedef struct{
int age;
float height;
} person_s; //creates a new type "person_s"
person_s person1; //illustrates using new type to create an instance of the struct
...and one similar to your unnamed struct: (leaving off the pointer instance for simplification)
struct{
int age;
float height;
}person1;// create single instance of struct, but this instance
// cannot be used to create new instances
... is that the typedef
version creates a new data type person_s
. person_s
can be used to create new instances of the struct anywhere in your code, within the scope in which it was created.
While the unnamed struct
creates only one instance of the unnamed struct. And while the instance person1
can be used and referenced in several places of your code (limited only by the scope in which it was created.) but no new instances of the unnamed struct can be created beyond that initial instance person1
.
So in summary,
person1
is a single instance of the unnamed struct and person_s
is a brand new type. The new type can be used to create new instances of that variable like any other type. (eg int
, float
, ...), but no new instances of the unnamed struct can be created.
Upvotes: 0
Reputation: 310980
In this declaration
struct{
int age;
float height;
}person1,*personPtr;
there are declared an unnamed structure an object of the structure type and a pointer to an object of the structure type.
You will be unable to refer the structure type in your program because it is unnamed.
These declarations
struct person{
int age;
float height;
};
struct person person1;
struct person *personPtr;
differ from the preceding declaration in the way as there is declared a named structure that can be referred in the program.
The code snippet would be equivalent if the first declaration will be rewritten like
struct person{
int age;
float height;
}person1,*personPtr;
Upvotes: 4