title: C program football team structure with pointer elements and dynamic allocation
body: <p>I have a question about passing function to another function which both have structure as arguments. First I created two structures:</p>
  
  <pre><code>typedef struct
  {
      char name[25],surname[25];int number;
  }PLAYER;
  
  typedef struct
  {
      char nameofteam[25];int numberofplayers;char *players;
  }TEAM;
  </code></pre>
  
  <p>Then I defined a function to read elements of one player:</p>
  
  <pre><code>void readplayer(PLAYER *);
  void readplayer(PLAYER *pi)
  {
      printf("name:");scanf("%s",pi-&gt;name);
      printf("surname:");scanf("%s",pi-&gt;surname);
      printf("number of player:");scanf("%d",&amp;pi-&gt;number);
  }
  </code></pre>
  
  <p>My question is how to create function which prototype is <code>void readteam(TEAM*)</code> which will read data for one team, but using function <code>readplayer</code> and call it in <code>main()</code>? Here is what I have tried:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  typedef struct
  {
      char name[25],surname[25];int number;
  }PLAYER;
  
  typedef struct
  {
      char nameofteam[25];int numberofplayers;char *players;
  }TEAM;
  void readplayer(PLAYER *pi)
  {
      printf("name:");scanf("%s",pi-&gt;name);
      printf("surname:");scanf("%s",pi-&gt;surname);
      printf("number of player:");scanf("%d",&amp;pi-&gt;number);
  }
  void readteam(TEAM *pt)
  {
   char players[101];int i;
   printf("name of team:");scanf("%s",pt-&gt;nameofteam);
   printf("number of players in team:");scanf("%d",&amp;pt-&gt;numberofplayers);
   printf("players:");scanf("%s",players);
   pt-&gt;players=(char *)calloc(length(players)+1,sizeof(char));
   copy(pt-&gt;players,players);
   for(i=0;i&lt;pt-&gt;numberofplayers;i++)
   {
       printf("%d.",i+1);
       readplayer(pt+i);
   }
  }
  void erase(TEAM *);
  void erase(TEAM *pt)
  {
      free(pt-&gt;players);
  }
  int length(char *s)
  {
      int d=-1;
      while(s[++d]);
      return d;
  }
  
  void copy(char *s1,char *s2)
  {
      while(*s1++ = *s2++);
  }
  int main()
  {
   int i,n;
   TEAM *p;
   do
   {
       printf("n=");scanf("%d",&amp;n);
   }
   while(n&lt;1);
   p=(TEAM *)malloc(n * sizeof(TEAM));
   for(i=0;i&lt;n;i++)
   {
       printf("%d.",i+1);readteam(p+i);
   }
   free(p);
  }
  </code></pre>
  
  <p>This gives me an error at the last input (in compiling, not debugging). Must be because of inappropriate use of dynamic allocation. I didn't use <code>&lt;string.h</code> library. Obviously, only the <code>readteam</code> function has to be in <code>main()</code>.</p>
  
  <p>Thanks for the answers.</p>