title: Dynamically allocating an array of structs
body: <p>I'm trying to create an array of structs, of which the array size is defined by the user in the program. eg. p[0], p[1], p[2].....</p>
  
  <pre><code>typedef struct
  {
  int score;
  }player;
  
  void main()
  {
      int numPlayers;
  
      printf ("\nEnter number of players (1 - 4)\n");
      scanf ("%d", &amp;numPlayers);
  
  }
  </code></pre>
  
  <p>I've tried doing it with both</p>
  
  <pre><code>player p[numPlayers];
  </code></pre>
  
  <p>and</p>
  
  <pre><code>player *p=malloc(numPlayers*sizeof(player));
  </code></pre>
  
  <p>but both won't compile.
  Can anyone see what's going wrong here?</p>
  
  <p>Edit: I'm using VS2010.
  I'm getting "expression must have a constant value" for the first one, and "a value of type "void*" cannot be used to initialise an entity of type "player*" for the second one.</p>