Reputation: 3871
I have to declare an array of structures of size 16. The following code gives an error code1.c:12:1: error: initializer element is not constant
typedef struct node
{
int tokenvalue;
struct node *next;
char *n;
} node;
node *dummy=(node *)malloc(26*sizeof(node));
Also using node dummy[26] gives segmentation fault. What should I do?
Upvotes: 1
Views: 148
Reputation: 23268
Try initializing it in your main()
function as you cannot initialize global variables with non-constant values or values that cannot be determined at compile time.
Alternatively you can declare it as
node dummy[27];
as a global variable instead of having to use malloc (if the size is constant).
Upvotes: 4
Reputation: 182649
You can't initialize objects having static storage with anything non compile-time constant. Leave it uninitialized and assign some memory to it in a function.
Upvotes: 6