-1

I have a struct that I don't understand :

typedef struct {
    int x;
    int y;
} Position; 

typedef struct {
    int id;
    Position upper_left;
    Position lower_right;
    int priority;
} *Window_Description;

I don't understand why the struct *Window_Description has an asterisk before with? Is it a pointer to the structure? Because when I will create some Window_Description, it will be a pointer?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Gregory
  • 19
  • 1
  • 8

1 Answers1

1

The definition

typedef struct {
    int id;
    Position upper_left;
    Position lower_right;
    int priority;
} *Window_Description;

is equal to

struct Window_Description_Struct
{
    int id;
    Position upper_left;
    Position lower_right;
    int priority;
};

typedef struct Window_Description_Struct *Window_Description;

That is, it makes Window_Description an alias for a pointer to the structure.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Okay so would i be able to declare structures with Window_Description w = {...}; ? If so, why create it as a pointer to the structure ? Why not just declare it only as a structure ? Thank you – Gregory Mar 07 '15 at 04:18
  • @CptDesro No, because `Window_Description` is a *pointer* type. You might however do e.g. `Window_Description w = malloc(sizeof *w);` As for why declare it as a pointer type? I really don't know, and I generally advice you to not do it as it tends to hide the fact that the type is a pointer type. – Some programmer dude Mar 07 '15 at 04:20
  • Its a example from my teacher... which i don't really understand. So if i want to create a Window_Description ? How i declare it ? – Gregory Mar 07 '15 at 04:28
  • @CptDesro You declare a pointer to the structure by using the alias `Window_Description`, that's all there is to it. With the structure and typedef declaration as shown in your question, there is no way of actually declaring an actual instance of the structure, just pointers to it. If you want more help, you should really ask your teacher. – Some programmer dude Mar 07 '15 at 04:35