0

So my teacher gave me slides bout how to build a list(or a node) and i understood the most of it. The problem is that it's not very clear the lexicon of lists.

T_Player* make_player(char* name, char* lastn, T_Ruolo role, int number){
    T_Player* player = (T_Player *) malloc(sizeof (T_Player));
    assert(player != NULL);

    strcpy(player->name, name);
    strcpy(player->lastn, lastn);
    player->number = number;
    player->role = role;

    return player;
}

First of all why after T_Player there's the operator * . when i used to make a function i always wrote it like:

int Name_ofFunction(Type variable,Type Variable2); //random prototype 

now the * operator cofuses me. IT's used to point to something . What does that point to ? 'Couse after the* there's the name of the function,which is not a variable. So why that * is there whats the use for ?

  • 2
    `(T_Player *)` in front of `malloc` is not required and considered bad practice - see [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed Heal Nov 28 '19 at 15:23
  • 2
    "T_Player* make_player(etc etc..." indicates that the function make_player returns a pointer to a T_Player data type. At the end of the function you see it returns 'player' which was declared as a "T_Player * ", i.e. a pointer to a T_Player. – Simon F Nov 28 '19 at 15:25
  • 3
    ... so the `*` is *not* an operator in that context. It's part of the syntax for a pointer type. – John Bollinger Nov 28 '19 at 15:26
  • 1
    That's basic knowledge you're asking about. Please read the chapter dealing with pointers in your beginner's C text book. – Jabberwocky Nov 28 '19 at 16:14
  • https://beginnersbook.com/2014/01/c-pointers/; instead of returning a copy of the entire `struct`, they return a pointer to a dynamically allocated block. This is very normal. – Neil Nov 28 '19 at 19:00

0 Answers0