title: Can't figure out what causes the segmentation fault | from HW
body: <p>I have the following data types:</p>
  
  <h2>formation.h</h2>
  
  <pre><code>typedef struct formation_t {
      Player players[FORMATION_NUM_PLAYERS];
      int numPlayers;                /* How many players are in the above array */
      int timesPlayed;
      int timesWon;
  }* Formation;
  </code></pre>
  
  <hr>
  
  <h2>team.h</h2>
  
  <pre><code>typedef struct team_t {
      char* name;
      char* coachName;
      Formation* formations;
      int currFormations;
      int maxFormations;
  }* Team;
  </code></pre>
  
  <hr>
  
  <p>And the following functions:</p>
  
  <hr>
  
  <pre><code>Team teamCreate(char* name, char* coach, int maxFormations)
  {
      //Check if parameters make sense.
      if (name == NULL || coach == NULL || maxFormations &lt; 1)
      {
          return NULL;
      }
  
      //Try allocating memory for name.
      char* teamName = malloc(strlen(name) + 1);
      if (teamName == NULL)
      {
          return NULL;
      }
      strcpy(teamName, name);
  
      //Try allocating memory for coachName.
      char* coachName = malloc(strlen(coach) + 1);
      if (coachName == NULL)
      {
          free(teamName);
          return NULL;
      }
      strcpy(coachName, coach);
  
      //Try allocating memory for formations.
      Formation* formations = malloc(sizeof(Formation) * maxFormations);
      if (formations == NULL)
      {
          free(teamName);
          free(coachName);
          return NULL;
      }
  
      //Try allocating memory for team.
      Team newTeam = malloc(sizeof(struct team_t));
      if (newTeam == NULL)
      {
          free(teamName);
          free(coachName);
          free(formations);
          return NULL;
      }
  
      //Initialize newly created team.
      newTeam-&gt;name = teamName;
      newTeam-&gt;coachName = coachName;
      newTeam-&gt;maxFormations = maxFormations;
      newTeam-&gt;currFormations = 0;
  
  
      //Return created team.
      return newTeam;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>TeamResult teamAddFormation(Team team, Formation formation)
  {
      //Check for TEAM_NULL_ARGUMENT.
      if (team == NULL | formation == NULL)
      {
          return TEAM_NULL_ARGUMENT;
      }
  
      //Check for TEAM_IS_FULL.
      if (team-&gt;currFormations == team-&gt;maxFormations)
      {
          return TEAM_IS_FULL;
      }
  
      //Add formation.
      printf("\n -about to clone- \n");
      team-&gt;formations[team-&gt;currFormations] = formationClone(formation);
      printf("\n -clone completed- \n");
      team-&gt;currFormations = team-&gt;currFormations + 1;
  
      return TEAM_SUCCESS;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>Formation formationClone(Formation formation)
  {
      if (formation == NULL)
      {
          return NULL;
      }
  
      Formation newFormation = malloc(sizeof(struct formation_t));
      if (newFormation == NULL)
      {
          return NULL;
      }
  
      *newFormation = *formation;
  
      return newFormation;
  }
  </code></pre>
  
  <hr>
  
  <p>When I try to test my work using the following code, I get a segmentation fault right after "about to clone".</p>
  
  <pre><code>Team team = teamCreate("Ac Milan", "Carletto", 2);
  
  Formation formation1 = formationCreate();
  ASSERT_NULL_ARGUMENT(teamAddFormation(NULL, formation1));
  ASSERT_SUCCESS(teamAddFormation(team, formation1));
  </code></pre>