Initialize array of structs - c code error

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;
}

Answers


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;
}

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));.


Need Your Help

Error installing Grails with GVM - Curl issue

grails gvm

I am having trouble installing Grails via GVM. I installed GVM via the instructions on GVM's website, and it appears it was installed correctly - restarting the terminal and running gvm help produc...