Search code examples
cfunctionpointersstructure

Pointer to a type-defined structure


Okay so I have a following code that was previously written. This is for an assignment that I am doing and I cannot change these lines of code:

typedef struct
{
    int number;
    float average;

} B_PLAYER;


typedef struct
{
    char n[20];
    B_PLAYER k[12];

} TEAM;

And also two declared functions which look like this:

int result (TEAM t1, TEAM t2); 

↑ exclude this function for now, this calculates the result of a match

TEAM* createTeam();

The code I must write is supposed to simulate a basketball match (multiple matches with 32 teams in total). Because the assignment is too long (and a bit complex), I will keep it short. So the the first part of the code above defines types named B_PLAYER and TEAM, which I understand. But my question is, why is the type name TEAM used as a function return type and what does the pointer to that type name mean(do):

TEAM* createTeam(); <------------ This

Keep in mind that, this function needs to create an address of a basketball team and return that same address. It must read values from the main() function(standard input). All 12 positions and all kit numbers must be filled.

I would gladly appreciate some feedback. If you need more information, I will write the whole assignment. :)


Solution

  • But my question is, why is the type name TEAM used as a function return type and what does the pointer to that type name mean(do):

    TEAM* createTeam(); <------------ This

    You are reading it wrong. TEAM is not used as a function return type!

    TEAM* is to be read as "one thing" - you cant break into two like TEAM with one meaning and * with another meaning. It has to be read as TEAM* which means a pointer to a TEAM object.

    So TEAM* createTeam(); means that you must return the value of a pointer to a TEAM object. In short we just say: "return a pointer to a TEAM object" simply because it's implicit in C that functions always return values.

    So how can you get a TEAM object and return a pointer to it inside a function?

    The answer is malloc (and friends). Like:

    TEAM* createTeam()
    {
        TEAM* team_ptr;                       // Create a pointer to a TEAM object
        team_ptr = malloc(sizeof *team_ptr);  // Allocate memory for a TEAM object and
                                              // assign the address of that memory
                                              // to the pointer
        if (team_ptr == NULL) exit(1);        // Error checking
        return team_ptr;                      // Return (the value of) the pointer to the TEAM object
    }
    

    The in e.g. main you can do:

    TEAM* teamA = createTeam();
    TEAM* teamB = createTeam();
    

    Now you have 2 pointers to differen TEAM objects.

    But wait... they have no values. No, you need to add some code to read values that you want to assign to the TEAM objects. Maybe like this:

    TEAM* createTeam()
    {
        TEAM* team_ptr;
        team_ptr = malloc(sizeof *team_ptr);
        if (team_ptr == NULL) exit(1);
    
        // Read team name
        fgets(team_ptr->n, sizeof team_ptr->n, stdin);
        size_t len = strlen(team_ptr->n);
        if (len > 0) team_ptr->n[len-1] = '\0';  // remove newline if present
    
        // Read the players
        for (int i = 0; i < 12; ++i)
        {
            team_ptr->k[i].number = 42;    // Add your own code to get it from stdin
            team_ptr->k[i].average = 42.0; // Add your own code to get it from stdin
        }
        return team_ptr;
    }
    

    tip: fgets and sscanf could be useful for the "missing" code.

    So now you can do like this in e.g. main :

    TEAM* teamA = createTeam();
    TEAM* teamB = createTeam();
    printf("Next match will be %s playing %s\n", teamA->n, teamB->n);
    printf("Player number %d at team %s has average %f\n", teamA->k[0].number, teamA->n, teamA->k[0].average);
    

    And once you are done with the teams you do:

    free(teamA);
    free(teamB);