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;
}
4
  • is PlayerList a defined type, which you're also trying to use as a variable? What is MAX defined as? Commented May 2, 2013 at 0:09
  • MAX is a defined number. Commented May 2, 2013 at 0: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). Commented May 2, 2013 at 0:13
  • #define MAX = 200; ----> #define MAX 200 Commented May 2, 2013 at 0:36

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

I see. But I need PlayerList to be a global, how would do that.
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.
You need to show your full code, then. complete, compilable, minimal code sample.
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.
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.
|
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;
}

10 Comments

Can I make this global some how?
@Morki note: You could not write executable statements outside a function.
Can I do something like static PlayerList = malloc(MAX * sizeof(Player));
inside a function, yes. But it's not the same thing.
@Morki I think that you cannot even static, but do not you mean C++?  It is possible if C++ is an explicit cast is required in that case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.