Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

int allocate_memory(player_t** player_p_p, long int nplayers) { /* This function

ID: 3742125 • Letter: I

Question

int allocate_memory(player_t** player_p_p, long int nplayers)

{

/*

This function allocates memory for a player_t pointer variable.

Inputs:

player_p_p - memory location of a player_t pointer variable

nplayers - number of players that memory needs to be allocated for

Return:

0 - success

1 - failed to allocate memory

Post:

After the function has been called, *player_p_p will point to freshly

allocated memory if malloc was successful. Otherwise *player_p_p will point to

NULL.

*/

return 1; /* MODIFY */

}

void init_player(player_t* player_p, int age, int wickets)

{

/*

This function initialises each field of one playet_t struct.

Inputs:

player_p - memory location of the player_t variable. player_p must have been

allocated memory using allocate_memory before calling this function.

nplayers - number of players that memory needs to be allocated for

Post:

After the function has been called, the age field of *player_p will be the

input age, and wickets field of *player_p will be the input wickets.

*/}

void print_player(player_t player)
{
/*
This function prints a player_t variable in the following format:
player - age:AA wickets:BBB
Inputs:
player_p - variable to be printed
*/
}

Explanation / Answer

int allocate_memory(player_t** player_p_p, long int nplayers)

{

    /*

        This function allocates memory for a player_t pointer variable.

        Inputs:

        player_p_p - memory location of a player_t pointer variable

        nplayers - number of players that memory needs to be allocated for

        Return:

        0 - success

        1 - failed to allocate memory

        Post:

        After the function has been called, *player_p_p will point to freshly

        allocated memory if malloc was successful. Otherwise *player_p_p will point to

        NULL.

    */

   

    // allocate memory using malloc

    player_p_p = ( player_t ** )malloc( nplayers * sizeof( player_t * ) );

    // if failed to allocate memory

    if( player_p_p == NULL )

        return 0;

   

    return 1; /* MODIFY */

}

void init_player(player_t* player_p, int age, int wickets)

{

    /*

        This function initialises each field of one playet_t struct.

        Inputs:

        player_p - memory location of the player_t variable. player_p must have been

        allocated memory using allocate_memory before calling this function.

        nplayers - number of players that memory needs to be allocated for

        Post:

        After the function has been called, the age field of *player_p will be the

        input age, and wickets field of *player_p will be the input wickets.

    */

   

    // allocate mempry to player_p

    player_p = ( player_t * )malloc( sizeof(player_p) );

   

    player_p->age = age;

    player_p->wickets = wickets;

}

void print_player(player_t player)

{

    /*

        This function prints a player_t variable in the following format:

        player - age:AA wickets:BBB

        Inputs:

        player_p - variable to be printed

    */

   

    printf("player - age:%f wickets:%f" , player.age , player.wickets);

}