Abhishek Ghosh
Abhishek Ghosh

Reputation: 665

An insight into the error "initializer element is not constant"

Behold the following code... I am getting the said error for line no. 6... Pls can anyone explain as to why is this happening?

#include<stdio.h>

struct test{
    int data;
};

typedef struct test* Test;

Test obj=(Test) calloc(1,sizeof(struct test));

int main()
{
    return 0;
}

Upvotes: 1

Views: 116

Answers (1)

dbush
dbush

Reputation: 223787

The variable obj resides at file scope, so it's initializer must be a compile time constant. You're attempting to call a function instead. That's not allowed, as that would otherwise mean code would be run (in this case calling a function) outside of a function.

You would need to move the code that assigns a value into the main function.

#include<stdio.h>

struct test{
    int data;
};

typedef struct test* Test;

Test obj;

int main()
{
    obj = calloc(1,sizeof(struct test));
    return 0;
}

Upvotes: 5

Related Questions