We know that struct objects can be declared at the end of structure definition like so :

struct vertex
{
float x,y,z;
}v1;

Is such a declaration of an object like v1 possible while using typedef struct?

typedef struct vertex
{
float x,y,z;
} vertex;

Is it mandatory to declare objects separately now with

vertex v1;

can they not be appended at the end of struct definition in this case?

2 Answers

2
Sourav Ghosh On Best Solutions

No, it cannot be.

typedef is used for creation of an alias / synonym for another type. It cannot be used for declaration of variables.

typedef struct ver
{
float x,y,z;
} vertex;

Here, vertex is same as struct ver (I changed the name slightly for better understanding).

Once the type (alias) is created, you use that to create a variable using another identifier, like

vertex v;
struct ver v1;
2
Jonathan Leffler On

If you use the 'storage class' typedef, then the names defined after the structure are the typedef names — aliases for the structure type. So, vertex is a type name. When you define the type name, you cannot also define any variables.