title: How can I write to char in structure with sprintf
body: <p>I wrote this program for school but even if it finishes, dev-cpp gives me a SIGSEGV error at the end.
  By searching on google i found that my problem probably comes from everything that is bound to my chars in my structures.
  I am trying to write on my char nom[10] and char nom_team[10] with sprintf.
  I do this because I want to ad a number to the default "player" name like so :
  player 1, player 2,...</p>
  
  <p>I don't know exactly where I am trying to write on memory that I don't own.
  I don't know any other way to achieve the player 1, player 2,... thing either.</p>
  
  <p>Thank you for the help.</p>
  
  <p>EDIT (full code &amp; edit following Michael Walz comment)</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  typedef struct player player; //shortened Perso type
  typedef struct team Team; //shortened Team type
  
  struct player //perso structure
  {
      char name[10]; //player name
      int HP; //healt points
      int ATK; //attack points
      int DEF; //defense points
  };
  
  struct team //team structure
  {
      char name_team[10]; //team name
      player player[5]; //players in the team
      int nbrP; //number of players left
  };
  
  void init_player (player*); //player initialisation
  void fight (player*,player*); //damage calculation
  int HPP (player*,player*); //players duel simulation
  void init_team (Team*); //team initialisation
  char* TvT (Team*,Team*); //team combat simulation
  
  main() //main
  {
      srand(time(NULL));
      int i;
      char* wint; //winning team name string
      Team t[2]; //the 2 teams
      for (i=1;i&lt;3;i++)
      {
          init_team (&amp;t[i]); //teams initialisation
      }
      //changing team names
      sprintf(t[1].name_team,"%s","Orange");
      sprintf(t[2].name_team,"%s","Verte");
      //changing players names
      sprintf(t[1].player[0].name,"%s","Gerald");
      sprintf(t[1].player[1].name,"%s","Ilda");
      sprintf(t[1].player[2].name,"%s","Falion");
      sprintf(t[1].player[3].name,"%s","Thor");
      sprintf(t[1].player[4].name,"%s","Ralof");
      sprintf(t[2].player[0].name,"%s","Gandalf");
      sprintf(t[2].player[1].name,"%s","Jaena");
      sprintf(t[2].player[2].name,"%s","Philip");
      sprintf(t[2].player[3].name,"%s","Astrid");
      sprintf(t[2].player[4].name,"%s","Ponchaut");
      wint = TvT (&amp;t[1],&amp;t[2]); //wint is the result of the combat between the two teams  
      printf("%s\n",wint);
  }
  
  void init_player (player* p) //player initialisation
  {
      static int i=1; //static variable for player number
      sprintf(p-&gt;name,"player %d",i); //applying player name
      p-&gt;HP = 100 + rand()%10; //giving HP
      p-&gt;ATK = 10 + rand()%10; //giving ATK
      p-&gt;DEF = 5 + rand()%5; //giving DEF
      printf("name : %s\n",p-&gt;name);
      printf("HP : %d\n",p-&gt;HP);
      printf("ATK : %d\n",p-&gt;ATK);
      printf("DEF : %d\n",p-&gt;DEF);
      printf("\n");
      //showing player stats
      i++; //next player number
  }
  
  void fight (player *p1,player *p2) //damage calculation
  {
      int DAM;
      DAM = p1-&gt;ATK - p2-&gt;DEF; //simple damage calculation
      printf("DAM : %d\n",DAM); //showing damage done
      p2-&gt;HP = p2-&gt;HP - DAM; //applying damage
  }
  
  int HPP (player *p1,player *p2) //players duel simulation
  {
      int turnp; //turns of the duel
      for (turnp=1;p1-&gt;HP&gt;0 &amp;&amp; p2-&gt;HP&gt;0;turnp++) //continues while players are alive
      {
          printf("HP p1 : %d\n",p1-&gt;HP); //showing hp before combat
          printf("HP p2 : %d\n",p2-&gt;HP);
          if(turnp%2 != 0) fight (p1,p2); //p1 always begin
          else fight (p2,p1); //then it's p2's turn to attack
      }
      if (p1-&gt;HP&lt;=0) return 2; //p2 is the winner if p1 dies
      else return 1; //p1 is the winner is p2 dies
  }
  
  void init_team (Team* t) //team initialisation
  {
      int i;
      static int k=1; //static variable for team number
      sprintf(t-&gt;name_team,"team %d",k); //applying team name
      t-&gt;nbrP = 5; //defining number of players left in team at the start
      for (i=0;i&lt;5;i++)
      {
          init_player(&amp;t-&gt;player[i]); //initializing players in team
      }
      k++; //next team number
  }
  
  char* TvT (Team* t1, Team* t2) //team combat simulation
  {
      int turnt,winp; //team combat turns and duel winner variables
      int i=0,j=0;
      for (turnt=1;t1-&gt;nbrP&gt;0 &amp;&amp; t2-&gt;nbrP&gt;0;turnt++) //continues while the teams have at least one player left alive
      {
          printf("%s de la team %s contre %s de la team %s\n",t1-&gt;player[i].name,t1-&gt;name_team,t2-&gt;player[j].name,t2-&gt;name_team);
          //introduction message to next duel
          winp =  HPP (&amp;t1-&gt;player[i],&amp;t2-&gt;player[j]); //simulating duel, the result is stored in winp
          if (winp == 1)
          {
              (t2-&gt;nbrP)--; //if player in team 2 dies we decrement the number of players left in that team
              j++; //we order the next player in the team 2 to duel
          }
          else
          {
              (t1-&gt;nbrP)--; ///if player in team 1 dies we decrement the number of players left in that team
              i++; //we order the next player in the team 1 to duel
          }
      }
      if(t1-&gt;nbrP&lt;=0) return t2-&gt;name_team; //if team 1 loses we return team 2's name
      else return t1-&gt;name_team; //if team 2 loses we return team 1's name
  }
  </code></pre>
  
  <p>NOTE : When I put</p>
  
  <pre><code>char name[100]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>char name_team[100]
  </code></pre>
  
  <p>I get only this</p>
  
  <pre><code>name : player 1
  HP : 103
  ATK : 19
  DEF : 9
  
  name : player 2
  HP : 100
  ATK : 12
  DEF : 6
  
  name : player 3
  HP : 107
  ATK : 12
  DEF : 8
  
  name : player 4
  HP : 101
  ATK : 19
  DEF : 6
  
  name : player 5
  HP : 108
  ATK : 17
  DEF : 8
  
  
  --------------------------------
  Process exited after 2.507 seconds with return value 3221225477
  Appuyez sur une touche pour continuer...
  </code></pre>
  
  <p>The result I want is this (except that when t2 wins is writen "@■b" at the the bottom instea of "Verte" :</p>
  
  <pre><code>name : player 1
  HP : 109
  ATK : 14
  DEF : 9
  
  name : player 2
  HP : 106
  ATK : 12
  DEF : 8
  
  name : player 3
  HP : 106
  ATK : 17
  DEF : 8
  
  name : player 4
  HP : 100
  ATK : 11
  DEF : 8
  
  name : player 5
  HP : 103
  ATK : 12
  DEF : 9
  
  name : player 6
  HP : 106
  ATK : 12
  DEF : 6
  
  name : player 7
  HP : 105
  ATK : 13
  DEF : 7
  
  name : player 8
  HP : 107
  ATK : 19
  DEF : 7
  
  name : player 9
  HP : 100
  ATK : 16
  DEF : 8
  
  name : player 10
  HP : 105
  ATK : 11
  DEF : 9
  
  Gerald de la team Orange contre Gandalf de la team Verte
  HP p1 : 109
  HP p2 : 106
  DAM : 8
  HP p1 : 109
  HP p2 : 98
  DAM : 3
  HP p1 : 106
  HP p2 : 98
  DAM : 8
  HP p1 : 106
  HP p2 : 90
  DAM : 3
  HP p1 : 103
  HP p2 : 90
  DAM : 8
  HP p1 : 103
  HP p2 : 82
  DAM : 3
  HP p1 : 100
  HP p2 : 82
  DAM : 8
  HP p1 : 100
  HP p2 : 74
  DAM : 3
  HP p1 : 97
  HP p2 : 74
  DAM : 8
  HP p1 : 97
  HP p2 : 66
  DAM : 3
  HP p1 : 94
  HP p2 : 66
  DAM : 8
  HP p1 : 94
  HP p2 : 58
  DAM : 3
  HP p1 : 91
  HP p2 : 58
  DAM : 8
  HP p1 : 91
  HP p2 : 50
  DAM : 3
  HP p1 : 88
  HP p2 : 50
  DAM : 8
  HP p1 : 88
  HP p2 : 42
  DAM : 3
  HP p1 : 85
  HP p2 : 42
  DAM : 8
  HP p1 : 85
  HP p2 : 34
  DAM : 3
  HP p1 : 82
  HP p2 : 34
  DAM : 8
  HP p1 : 82
  HP p2 : 26
  DAM : 3
  HP p1 : 79
  HP p2 : 26
  DAM : 8
  HP p1 : 79
  HP p2 : 18
  DAM : 3
  HP p1 : 76
  HP p2 : 18
  DAM : 8
  HP p1 : 76
  HP p2 : 10
  DAM : 3
  HP p1 : 73
  HP p2 : 10
  DAM : 8
  HP p1 : 73
  HP p2 : 2
  DAM : 3
  HP p1 : 70
  HP p2 : 2
  DAM : 8
  Gerald de la team Orange contre Jaena de la team Verte
  HP p1 : 70
  HP p2 : 105
  DAM : 7
  HP p1 : 70
  HP p2 : 98
  DAM : 4
  HP p1 : 66
  HP p2 : 98
  DAM : 7
  HP p1 : 66
  HP p2 : 91
  DAM : 4
  HP p1 : 62
  HP p2 : 91
  DAM : 7
  HP p1 : 62
  HP p2 : 84
  DAM : 4
  HP p1 : 58
  HP p2 : 84
  DAM : 7
  HP p1 : 58
  HP p2 : 77
  DAM : 4
  HP p1 : 54
  HP p2 : 77
  DAM : 7
  HP p1 : 54
  HP p2 : 70
  DAM : 4
  HP p1 : 50
  HP p2 : 70
  DAM : 7
  HP p1 : 50
  HP p2 : 63
  DAM : 4
  HP p1 : 46
  HP p2 : 63
  DAM : 7
  HP p1 : 46
  HP p2 : 56
  DAM : 4
  HP p1 : 42
  HP p2 : 56
  DAM : 7
  HP p1 : 42
  HP p2 : 49
  DAM : 4
  HP p1 : 38
  HP p2 : 49
  DAM : 7
  HP p1 : 38
  HP p2 : 42
  DAM : 4
  HP p1 : 34
  HP p2 : 42
  DAM : 7
  HP p1 : 34
  HP p2 : 35
  DAM : 4
  HP p1 : 30
  HP p2 : 35
  DAM : 7
  HP p1 : 30
  HP p2 : 28
  DAM : 4
  HP p1 : 26
  HP p2 : 28
  DAM : 7
  HP p1 : 26
  HP p2 : 21
  DAM : 4
  HP p1 : 22
  HP p2 : 21
  DAM : 7
  HP p1 : 22
  HP p2 : 14
  DAM : 4
  HP p1 : 18
  HP p2 : 14
  DAM : 7
  HP p1 : 18
  HP p2 : 7
  DAM : 4
  HP p1 : 14
  HP p2 : 7
  DAM : 7
  Gerald de la team Orange contre Philip de la team Verte
  HP p1 : 14
  HP p2 : 107
  DAM : 7
  HP p1 : 14
  HP p2 : 100
  DAM : 10
  HP p1 : 4
  HP p2 : 100
  DAM : 7
  HP p1 : 4
  HP p2 : 93
  DAM : 10
  Ilda de la team Orange contre Philip de la team Verte
  HP p1 : 106
  HP p2 : 93
  DAM : 5
  HP p1 : 106
  HP p2 : 88
  DAM : 11
  HP p1 : 95
  HP p2 : 88
  DAM : 5
  HP p1 : 95
  HP p2 : 83
  DAM : 11
  HP p1 : 84
  HP p2 : 83
  DAM : 5
  HP p1 : 84
  HP p2 : 78
  DAM : 11
  HP p1 : 73
  HP p2 : 78
  DAM : 5
  HP p1 : 73
  HP p2 : 73
  DAM : 11
  HP p1 : 62
  HP p2 : 73
  DAM : 5
  HP p1 : 62
  HP p2 : 68
  DAM : 11
  HP p1 : 51
  HP p2 : 68
  DAM : 5
  HP p1 : 51
  HP p2 : 63
  DAM : 11
  HP p1 : 40
  HP p2 : 63
  DAM : 5
  HP p1 : 40
  HP p2 : 58
  DAM : 11
  HP p1 : 29
  HP p2 : 58
  DAM : 5
  HP p1 : 29
  HP p2 : 53
  DAM : 11
  HP p1 : 18
  HP p2 : 53
  DAM : 5
  HP p1 : 18
  HP p2 : 48
  DAM : 11
  HP p1 : 7
  HP p2 : 48
  DAM : 5
  HP p1 : 7
  HP p2 : 43
  DAM : 11
  Falion de la team Orange contre Philip de la team Verte
  HP p1 : 106
  HP p2 : 43
  DAM : 10
  HP p1 : 106
  HP p2 : 33
  DAM : 11
  HP p1 : 95
  HP p2 : 33
  DAM : 10
  HP p1 : 95
  HP p2 : 23
  DAM : 11
  HP p1 : 84
  HP p2 : 23
  DAM : 10
  HP p1 : 84
  HP p2 : 13
  DAM : 11
  HP p1 : 73
  HP p2 : 13
  DAM : 10
  HP p1 : 73
  HP p2 : 3
  DAM : 11
  HP p1 : 62
  HP p2 : 3
  DAM : 10
  Falion de la team Orange contre Astrid de la team Verte
  HP p1 : 62
  HP p2 : 100
  DAM : 9
  HP p1 : 62
  HP p2 : 91
  DAM : 8
  HP p1 : 54
  HP p2 : 91
  DAM : 9
  HP p1 : 54
  HP p2 : 82
  DAM : 8
  HP p1 : 46
  HP p2 : 82
  DAM : 9
  HP p1 : 46
  HP p2 : 73
  DAM : 8
  HP p1 : 38
  HP p2 : 73
  DAM : 9
  HP p1 : 38
  HP p2 : 64
  DAM : 8
  HP p1 : 30
  HP p2 : 64
  DAM : 9
  HP p1 : 30
  HP p2 : 55
  DAM : 8
  HP p1 : 22
  HP p2 : 55
  DAM : 9
  HP p1 : 22
  HP p2 : 46
  DAM : 8
  HP p1 : 14
  HP p2 : 46
  DAM : 9
  HP p1 : 14
  HP p2 : 37
  DAM : 8
  HP p1 : 6
  HP p2 : 37
  DAM : 9
  HP p1 : 6
  HP p2 : 28
  DAM : 8
  Thor de la team Orange contre Astrid de la team Verte
  HP p1 : 100
  HP p2 : 28
  DAM : 3
  HP p1 : 100
  HP p2 : 25
  DAM : 8
  HP p1 : 92
  HP p2 : 25
  DAM : 3
  HP p1 : 92
  HP p2 : 22
  DAM : 8
  HP p1 : 84
  HP p2 : 22
  DAM : 3
  HP p1 : 84
  HP p2 : 19
  DAM : 8
  HP p1 : 76
  HP p2 : 19
  DAM : 3
  HP p1 : 76
  HP p2 : 16
  DAM : 8
  HP p1 : 68
  HP p2 : 16
  DAM : 3
  HP p1 : 68
  HP p2 : 13
  DAM : 8
  HP p1 : 60
  HP p2 : 13
  DAM : 3
  HP p1 : 60
  HP p2 : 10
  DAM : 8
  HP p1 : 52
  HP p2 : 10
  DAM : 3
  HP p1 : 52
  HP p2 : 7
  DAM : 8
  HP p1 : 44
  HP p2 : 7
  DAM : 3
  HP p1 : 44
  HP p2 : 4
  DAM : 8
  HP p1 : 36
  HP p2 : 4
  DAM : 3
  HP p1 : 36
  HP p2 : 1
  DAM : 8
  HP p1 : 28
  HP p2 : 1
  DAM : 3
  Thor de la team Orange contre Ponchaut de la team Verte
  HP p1 : 28
  HP p2 : 105
  DAM : 2
  HP p1 : 28
  HP p2 : 103
  DAM : 3
  HP p1 : 25
  HP p2 : 103
  DAM : 2
  HP p1 : 25
  HP p2 : 101
  DAM : 3
  HP p1 : 22
  HP p2 : 101
  DAM : 2
  HP p1 : 22
  HP p2 : 99
  DAM : 3
  HP p1 : 19
  HP p2 : 99
  DAM : 2
  HP p1 : 19
  HP p2 : 97
  DAM : 3
  HP p1 : 16
  HP p2 : 97
  DAM : 2
  HP p1 : 16
  HP p2 : 95
  DAM : 3
  HP p1 : 13
  HP p2 : 95
  DAM : 2
  HP p1 : 13
  HP p2 : 93
  DAM : 3
  HP p1 : 10
  HP p2 : 93
  DAM : 2
  HP p1 : 10
  HP p2 : 91
  DAM : 3
  HP p1 : 7
  HP p2 : 91
  DAM : 2
  HP p1 : 7
  HP p2 : 89
  DAM : 3
  HP p1 : 4
  HP p2 : 89
  DAM : 2
  HP p1 : 4
  HP p2 : 87
  DAM : 3
  HP p1 : 1
  HP p2 : 87
  DAM : 2
  HP p1 : 1
  HP p2 : 85
  DAM : 3
  Ralof de la team Orange contre Ponchaut de la team Verte
  HP p1 : 103
  HP p2 : 85
  DAM : 3
  HP p1 : 103
  HP p2 : 82
  DAM : 2
  HP p1 : 101
  HP p2 : 82
  DAM : 3
  HP p1 : 101
  HP p2 : 79
  DAM : 2
  HP p1 : 99
  HP p2 : 79
  DAM : 3
  HP p1 : 99
  HP p2 : 76
  DAM : 2
  HP p1 : 97
  HP p2 : 76
  DAM : 3
  HP p1 : 97
  HP p2 : 73
  DAM : 2
  HP p1 : 95
  HP p2 : 73
  DAM : 3
  HP p1 : 95
  HP p2 : 70
  DAM : 2
  HP p1 : 93
  HP p2 : 70
  DAM : 3
  HP p1 : 93
  HP p2 : 67
  DAM : 2
  HP p1 : 91
  HP p2 : 67
  DAM : 3
  HP p1 : 91
  HP p2 : 64
  DAM : 2
  HP p1 : 89
  HP p2 : 64
  DAM : 3
  HP p1 : 89
  HP p2 : 61
  DAM : 2
  HP p1 : 87
  HP p2 : 61
  DAM : 3
  HP p1 : 87
  HP p2 : 58
  DAM : 2
  HP p1 : 85
  HP p2 : 58
  DAM : 3
  HP p1 : 85
  HP p2 : 55
  DAM : 2
  HP p1 : 83
  HP p2 : 55
  DAM : 3
  HP p1 : 83
  HP p2 : 52
  DAM : 2
  HP p1 : 81
  HP p2 : 52
  DAM : 3
  HP p1 : 81
  HP p2 : 49
  DAM : 2
  HP p1 : 79
  HP p2 : 49
  DAM : 3
  HP p1 : 79
  HP p2 : 46
  DAM : 2
  HP p1 : 77
  HP p2 : 46
  DAM : 3
  HP p1 : 77
  HP p2 : 43
  DAM : 2
  HP p1 : 75
  HP p2 : 43
  DAM : 3
  HP p1 : 75
  HP p2 : 40
  DAM : 2
  HP p1 : 73
  HP p2 : 40
  DAM : 3
  HP p1 : 73
  HP p2 : 37
  DAM : 2
  HP p1 : 71
  HP p2 : 37
  DAM : 3
  HP p1 : 71
  HP p2 : 34
  DAM : 2
  HP p1 : 69
  HP p2 : 34
  DAM : 3
  HP p1 : 69
  HP p2 : 31
  DAM : 2
  HP p1 : 67
  HP p2 : 31
  DAM : 3
  HP p1 : 67
  HP p2 : 28
  DAM : 2
  HP p1 : 65
  HP p2 : 28
  DAM : 3
  HP p1 : 65
  HP p2 : 25
  DAM : 2
  HP p1 : 63
  HP p2 : 25
  DAM : 3
  HP p1 : 63
  HP p2 : 22
  DAM : 2
  HP p1 : 61
  HP p2 : 22
  DAM : 3
  HP p1 : 61
  HP p2 : 19
  DAM : 2
  HP p1 : 59
  HP p2 : 19
  DAM : 3
  HP p1 : 59
  HP p2 : 16
  DAM : 2
  HP p1 : 57
  HP p2 : 16
  DAM : 3
  HP p1 : 57
  HP p2 : 13
  DAM : 2
  HP p1 : 55
  HP p2 : 13
  DAM : 3
  HP p1 : 55
  HP p2 : 10
  DAM : 2
  HP p1 : 53
  HP p2 : 10
  DAM : 3
  HP p1 : 53
  HP p2 : 7
  DAM : 2
  HP p1 : 51
  HP p2 : 7
  DAM : 3
  HP p1 : 51
  HP p2 : 4
  DAM : 2
  HP p1 : 49
  HP p2 : 4
  DAM : 3
  HP p1 : 49
  HP p2 : 1
  DAM : 2
  HP p1 : 47
  HP p2 : 1
  DAM : 3
  Orange
  
  --------------------------------
  Process exited after 5.682 seconds with return value 3221225477
  Appuyez sur une touche pour continuer...
  </code></pre>
  
  <p>In either case, when I just compile and run the program using dev-cpp, windows tells me it crashed after everything is done.
  When I use the <a href="https://i.stack.imgur.com/4I1KW.png" rel="nofollow noreferrer">debug with -g argument</a></p>
  
  <p>I get SIGSEGV error at the end.</p>
  
  <p>NOTE 2 : We didn't learn snprintf yet so I am not allowed to use this command; there are no actual inputs; and about the line that gives the SIGSEGV error, I don't know how to ask dev-cpp to say which line it is, the only method I've got is to turn blocks of code into commentary (obviously keeping  a compilable program) until the error disappears and I led me to this line :</p>
  
  <pre><code>sprintf(p-&gt;name,"player %d",i); //applying player name
  </code></pre>