0

I have this code :

#define N 100  //starting size of the array

int is_full(VERTEX *arr);
int add_vertex(char *name);
int print_degree(int ID);
int _get_vertex(int ID);
VERTEX *resize_array(VERTEX *vertex_array,int new_size);

VERTEX *arr = (VERTEX*)calloc(N, sizeof(VERTEX)); // dynamic allocation and initialization to NULL\0

int main(void)
{
    int vertex_counter = 0 ;
    int starting_size_of_array = sizeof(VERTEX)*N;
}

I get the error : error C2099: initializer is not a constant

I want the VERTEX array to be global - in order for me to access this array anywhere . so how come it's not constant? N is under #define , and VERTEX has it's declaration in th .h file.

0

2 Answers 2

3

First off, the initialiser isn’t a constant. You need to initialise the global from within a function – e.g. main:

VERTEX *arr;

int main(void)
{
    arr = calloc(N, sizeof *arr);
}

But you should not use globals in the first place, if avoidable (and it usually is). It destroys your code design.

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

4 Comments

but then dor example if I have a function that adds a vertex , how can I reach this arr without passing it as a parameter?
@user1386966 You can’t, you pass it as a parameter. That’s the correct thing to do.
the problem is than my function is : void add_vertex(char * name) -homework stuff, so I can't pass another parameter
@user1386966 Ah. Sucks. In that case, take the solution I wrote in my answer.
0

The value calloc() will return is not constant. You can init arr to NULL then initialize it during your program start up.

Comments

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.