0

I have a struc called player, and I need to make an array of MAX players, so I based on the following page C - initialize array of structs, like so:

DEFINE MAX 200

typedef struct
{
   int ID;
} Player;

Player* PlayerList = malloc(MAX * sizeof(Player));

Problem is I keep getting the following error

error: expected expression before ‘=’ token
error: initializer element is not constant

Base code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX = 200;

typedef struct
{
    int ID;
} Player;

Player *PlayerList;

int start()
{
    PlayerList = malloc(MAX * sizeof(Player));
    return 1;
}

int main(int argc, char const *argv[])
{
    /* code */
    return 0;
}
Community
  • 1
  • 1
Morki
  • 215
  • 1
  • 3
  • 11
  • is `PlayerList` a defined type, which you're also trying to use as a variable? What is `MAX` defined as? – sapi May 02 '13 at 00:09
  • MAX is a defined number. – Morki May 02 '13 at 00:10
  • Please post short but complete examples - other people should be able to paste your code listing to a file and compile it without changes (or fail to compile, but with exactly the same errors). – Staven May 02 '13 at 00:13
  • `#define MAX = 200;` ----> `#define MAX 200` – BLUEPIXY May 02 '13 at 00:36

2 Answers2

2

You can't call malloc() from outside of any function. Just declare Player* PlayerList;, and let one of the first things you do in main() be PlayerList = malloc(MAX * sizeof(Player));.

This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
  • I see. But I need PlayerList to be a global, how would do that. – Morki May 02 '13 at 00:13
  • 1
    You can *declare* PlayerList outside of any function, but using a function call (`malloc()`) to initialize it, that can only be done in a context where function calls are valid. – This isn't my real name May 02 '13 at 00:16
  • You need to show your full code, then. complete, compilable, minimal code sample. – This isn't my real name May 02 '13 at 00:23
  • 1
    And there's the difference! `#define MAX = 200;` is not the same as `#define MAX 200`. remember, `#define` is a token substitution performed by a preprocessor. By the time the compiler tries to actually compile your code, you're now left with `PlayerList = malloc(= 200; * sizeof(Player));`, which I'm sure you'll agree is nonsensical. – This isn't my real name May 02 '13 at 00:35
  • Do you know how I would be able to free one player? free(PlayerList[2]) – Morki May 02 '13 at 00:50
  • 1
    Can't do that. You can only `free()` exactly the things you get from `malloc()`. You could set PlayerList[2] to some flag value indicating that it's invalid, or you could switch to an entirely different model, and use a linked link to keep your players in. – This isn't my real name May 02 '13 at 01:02
  • So the only way would be to do something like PlayerList[2].ID = NULL, and then reuse it later. Then wait until I want to free everything and call free(PlayerList)? – Morki May 02 '13 at 03:28
  • yeah, except for the NULL part. NULL is a pointer. Just use 0, or -1. – This isn't my real name May 02 '13 at 07:23
1

You can not use only constant for initialization in old type 'C'.

rewrite

Player* PlayerList = malloc(MAX * sizeof(Player));

to

Player* PlayerList;
PlayerList = malloc(MAX * sizeof(Player));

Add E.G.:

#include <stdlib.h>

#define MAX 200

typedef struct
{
   int ID;
} Player;

Player* PlayerList=NULL;

int main(void){
    PlayerList = malloc(MAX * sizeof(Player));
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70