Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have to make a program which loads a footbal team,with several players,and I have to make a substitution with one of the existing plaayers.
This is the function that needs to make the sustitution:
C
void substitution(TEAM *team, PLAYER player1, int n)
{
	int i;
	PLAYER *player;
	for (i = 1; i <= team->numberOfPlayers; i++)
	{
		if (&player[i].jerseyNumber == n)
		{
			player[i].jerseyNumber = player1[i].jerseyNumber;
			player[i].name= player1.name;
			player[i].surname = player1.surname;
		}
	}
}
Posted
Comments
Andreas Gieriet 19-Dec-15 13:08pm    
The Player *player is not initialized. Missing relation to the team variable...(?)
Additionally: what is the ownership rule of your pointer members of the Player struct? I.e. how do you destroy a Player* instance? Should player1 be a pointer? Otherwise it would be implicitly member-wise copied while passed to the function...
Finally, your if condition contains a "strange" & prefix - why? What do you try to achieve with that operator there?
Cheers
Andi
Dragan Zrilic 19-Dec-15 13:18pm    
No, player1 should not be a pointer.
The program has to change,for example, the player with the jersey no. 10 with player1,so that when I print the team after the substitution player1 has to be in the team thats why I want to initialize the values of player1 to the values of the player with jersey no. 10.
Andreas Gieriet 19-Dec-15 16:42pm    
Did you understand my question on ownership at all? This is the crucial part for a working program. The rest is syntax.
Cheers
Andi

1 solution

I really recommand that you stick to object oriented design pattern. It helps you to write clearer and better reusable code.

When you change a player than change the complete player object in the team.
Then it also easy:
C++
PLAYER *removed = team.players[n];//assuming players is an array and n is the index
delete removed;// free memory from object
team.players[n] = new PLAYER(sSurName,sPreName,n);//create object with constructor and  index n


Please take a look at Structs And Pointers. Compile and debug it for learning ;-)
 
Share this answer
 
Comments
Andreas Gieriet 19-Dec-15 16:43pm    
The OP asks for a C solution, not C++.
Cheers
Andi
Dragan Zrilic 20-Dec-15 3:29am    
I cannot use OO programming in this example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900