Reputation: 125
You can define a Point struct in this way:
typedef struct
{
int x, y;
} Point;
and also in this way:
typedef struct Point
{
int x, y;
};
What is the difference?
Upvotes: 0
Views: 137
Reputation: 1139
The second example, the typedef
statement have no effect. The compiler will probably ignore it or give you a warning.
What differs from this code:
typedef struct Point
{
int x, y;
} Point;
This allow you to use Point as a type, or as a struct. I consider a bad practice to use the struct name as a type, or even as a variable, but you are allowed to do this.
Upvotes: 1
Reputation: 7610
Consider the C
code given below,
typedef struct
{
int x, y;
} Point;
int main()
{
Point a;
a.x=111;
a.y=222;
printf("\n%d %d\n",a.x,a.y);
}
The above code will execute without any errors or warnings, whereas the following C
code will give you an error (error: 'Point' undeclared
) and a warning (warning: useless storage class specifier in empty declaration
).
typedef struct Point
{
int x, y;
};
int main()
{
Point a;
a.x=111;
a.y=222;
printf("\n%d %d\n",a.x,a.y);
}
To correct the error you have declare the structure variable a
as follows,
int main()
{
struct Point a;
a.x=111;
a.y=222;
printf("\n%d %d\n",a.x,a.y);
}
Upvotes: 1