1

Is there a practical difference between:

typedef struct
{
    float x;
    float y;
}   Point;

Point origin = {0.0, 0.0};

struct Point
{
    float x;
    float y;
}

struct Point origin = {0.0, 0.0};

Are both of these correct?

1
  • As I know, you need to assign values to the struct variable fields explicitly: origin.x = 0.0; and origin.y = 0.0; – user2477 Nov 14 '14 at 8:04
3

Yes. The first version

typedef struct
{
    // members    
} Point;

defines an anonymous struct (i.e., a struct that doesn't have a name) and gives it a type name (i.e., Point). Unfortunately, if you decide to have a pointer to a recursive member inside your struct this way, you won't be able to do that because as long as you're before the semicolon that ends the typedef statement, you won't be able to use the type name Point.

On the other hand,

typedef struct Point
{
    // members
} Point;

defines a struct named struct Point and gives it a type name Point. Both struct Point and Point can be used interchangeably. You'll also be able to create a recursive member inside your struct as follows

struct Point *pp;

Hope that helps!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .