0

What is the diferrence between these two and how are they used?

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

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

PS: I do not know what search term to use for this. (I'm sure this type of question has been answered before. Kindly add a link to an explanation/answer if known/available.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rohit Kumar J
  • 87
  • 1
  • 8
  • Note that it is permissible to use `typedef struct OneIdentifier { … } CompletelyDifferent;` too. However, C++ automatically makes the name without the `struct` prefix available, so writing `typedef struct SomeName { … } SomeName;` makes `SomeName` available in C — but only after the `typedef` statement is complete, so you can't use `SomeName *member;` in the structure definition, but you must still use `struct SomeName *member;`. Alternatively, use `typedef struct SomeName SomeName;` and then `struct SomeName { …; SomeName *member; … };` to define the structure type. The tag name is required. – Jonathan Leffler Oct 10 '22 at 19:53

1 Answers1

2

A struct declared as the first option can contain a pointer to itself.

You can do this:

typedef struct Name {
    struct Name *next;
    // ...
} Name;

But not this:

typedef struct {
    Name *next;
    // ...
} Name;

The typedef definition (Name, without the struct prefix) is only available after the typedef is complete. But struct Name can be used (though only as a pointer, being an incomplete type) before the struct definition is complete.

user16217248
  • 3,119
  • 19
  • 19
  • 37
  • Plus, second option is giving a name to an anonymous structure (but after it's been declared), so it can't be referenced inside itself. – AR7CORE Oct 11 '22 at 09:24