iaskdumbstuff
iaskdumbstuff

Reputation: 433

How does the typedef struct work syntactically?

I am new to this, so I apologize if I'm being dense. I'm having trouble understanding how to use typedef. Let's say I have the following command:

typedef struct {
    char pseudo[MAX_SIZE] ;
    int nb_mar ;
    } player ;

This creates a type called player which comprises of a chain of characters, and an int, correct?

Now, if we have this line of code:

struct set_of_players {
    player T[NB_MAX_JOUEURS] ;
    int nb ;
    }; 

I don't understand what this does? I would assume it creates a variable of type struct set_of_players, but what is it called? Usually when I see this kind of command, there's a word after the closed amplesand representing the variables name, but here it has nothing; I don't get it. Would this even work if I haven't done a typedef struct for set_of_players?

Finally, I have this line of code that I don't understand:

typedef struct set_of_players players;

I quite honestly have no idea what this even means. I'm assuming this is the typedef that I need to make the previous command make sense, but I don't even know what it does.

Again, I'm sorry if I'm looking at this the wrong way, and for the poor formatting. Any help would be greatly appreciated, thank you.

Upvotes: 5

Views: 1491

Answers (5)

Clifford
Clifford

Reputation: 93554

typedef is a very simple thing. Despite its name it does not define new types, but rather defines aliases (names) for existing types. Its syntax in all cases is:

typedef <existing-type> <type-alias>

In C a struct type is defined thus:

struct <tag> { <member-list> } ;

And its its type is struct <tag> which is more than you might want to type, and might regard as clutter. So it is a common idiom to create an alias so you do not need preceded the tag with struct everywhere. Hence:

typedef struct <tag> { <member-list> } <type-alias> ;

and

typedef struct <tag> <type-alias> ;

Both create an alias for struct <tag>, but in the first both the alias and the structure are defined in the same statement, and in the second an existing struct definition is used.

Given such a typedef, instead of declaring a structure variable thus...

struct <tag> structure_variable ;

... you can use:

<type-alias> structure_variable ;

which is just syntactic sugar - typedef does not affect the code behaviour or code generation.

Note that it is possible to define a typedef using an anonymous struct (one with no tag name), so that the structure may only be instantiated using the alias:

typedef struct { <member-list> } <type-alias> ;

In fact you only ever need a tag name if the structure is self referring - i.e. it contains pointers to a struct of its own type (as in a linked list for example), then:

typedef struct <tag>
{
    struct <tag>* self ;  // Cannot use <type-alias> here;
                          // it does not exist yet.
    ...
} <type-alias> ;

Upvotes: 1

Keith Thompson
Keith Thompson

Reputation: 263507

typedef and struct are two different kinds of declarations. struct defines a structure type. typedef define an alias for an existing type (it doesn't create a new type). They can be combined into a single declaration. Neither creates an object (variable) of the specified type.

struct player {
    char pseudo[MAX_SIZE];
    int nb_mar;
};

This is a structure definition. It creates a new type whose name is struct player.

typedef struct {
    char pseudo[MAX_SIZE];
    int nb_mar;
} player;

The tag in a structure definition is optional. If you omit it, the structure type is anonymous. Here we combine an anonymous structure definition with a typedef, making player an alias for the anonymous structure type.

typedef struct player {
    char pseudo[MAX_SIZE];
    int nb_mar;
} player;

Here we create a structure type named struct player and an alias for that type, player. (The syntax of a structure definition puts the tag immediately after the struct keyword; the syntax of a typedef puts the name at the end.)

We can now refer to the type either as struct player or as player

Here's another way to do the same thing:

struct player {
    char pseudo[MAX_SIZE];
    int nb_mar;
};
typedef struct player player;

With two separate declarations, we create a new type called struct player, and then we use the typedef to define a new name for that type, player.

In all these examples, I've used the same identifier for the structure tag and the typedef name. There's no requirement for them to be the same, and there are coding conventions where you might use struct player_s or typedef ... player_t. But since both names refer to the same type, there's no real reason to use different names. They should at least be related to each other in a simple and obvious way.

Note that a typedef for a structure type is never really necessary. You could just define the struct type without using a typedef, and consistently refer to the type as struct player. All the typedef does is define a new name for a type that already has a perfectly good name. (The advantage is that the new name is a single identifier; I personally don't see that as much of an advantage.)

Another point is that you need the structure tag if the structure contains a pointer to its own type:

typedef struct node {
    int data;
    struct node *next;
} node;

You have to use struct node in the declaration of next because the typedef name isn't visible yet. (There are ways around that using forward declarations, but I won't get into that.)

Upvotes: 0

Abhijit Pritam Dutta
Abhijit Pritam Dutta

Reputation: 5591

I am answering you after you have declared this line of code typedef struct set_of_players players;

So your code will work like this:-

suppose:-

 #define MAX_SIZE 20
 #define NB_MAX_JOUEURS 2

Now you can use both the type name like below in our codes:-

 typedef struct set_of_players players;
 players PLAYERS;
 strcpy(PLAYERS.T[0].pseudo, "something");
 PLAYERS.T[0].nb_mar = 1;

 struct set_of_players {
      player T[NB_MAX_JOUEURS] ;// reffering to the above example you have created a array of struct `player` and its size is 2
   int nb ;
  };

Also you can use the above struct like below:-

 struct set_of_players setplayers;
 strcpy(setplayers.T[0].pseudo, "something");
 setplayers.T[0].nb_mar = 1;

When you declare a type name to it you don't need to use struct set_of_players create a variable instead you will use the type name. That means you have given a specific name to your structure and in your case it was player and players respectively.

Upvotes: 0

dbush
dbush

Reputation: 224417

The typedef statement allows you to make an alias for the given type. You can then use either the alias or the original type name to declare a variable of that type.

typedef int number;

This creates an alias for int called number. So the following two statements declare variables of the same type:

int num1;
number num2;

Now using your example:

typedef struct {
    char pseudo[MAX_SIZE] ;
    int nb_mar ;
} player ;

This creates an anonymous struct and gives it the alias player. Because the struct doesn't have name, you can only use the alias to create a variable of that type.

struct set_of_players {
    player T[NB_MAX_JOUEURS] ;
    int nb ;
}; 

This creates a struct named struct set_of_players, but does not declare a typedef nor does it declare a variable of that type. Also notice that one of the fields is an array of type player, which we defined earlier. You can later declare a variable of this type as follows:

struct set_of_players my_set_var;

This line:

typedef struct set_of_players players;

Creates an alias for struct set_of_players called players. In this case the struct is not defined at the same time as the typedef but is defined elsewhere. So now you can declare a variable of this type like this:

players my_set_var;

Which is the same as the version using the full struct name.

Upvotes: 6

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

First syntax

typedef struct {
    char pseudo[MAX_SIZE] ;
    int nb_mar ;
    } player ;

This defines an unnamed struct. Then, with typedef, it creates an alias to this struct: player.

You can then declare your struct like this:

player my_player;

It is similar to this syntax:

typedef struct s_player {
  char pseudo[MAX_SIZE];
  int nb_mar;
} player;

In this second case, since your struct has a name, you can declare a player like this:

player my_player;
struct s_player my_player;

Second syntax

struct set_of_players {
  player T[NB_MAX_JOUEURS];
  int nb;
}; 

This defines a structure struct set_of_players. Since you don't have a typedef, you'll need to declare a variable of this type like this:

struct set_of_players my_set;

Third syntax

typedef struct set_of_players players;

This defines a type players that is an alias to the type struct set_of_players.

Because this creates an alias to struct set_of_players, it goes with the second syntax:

struct set_of_players {
  player T[NB_MAX_JOUEURS];
  int nb;
};

Upvotes: 3

Related Questions