Abdir
Abdir

Reputation: 87

Declaring array of structs

I have an array

int arr*;

I have declared a struct

struct counter{
    int index=0;
    int count=0;
    int *values;  // array
}

and the array will have a predefined max size.

How do I "push" structs inside each index of array? I have tried to do as following:

for ( int i =0; i < max ; i ++ ){
    arr[i]=counter Store_Struct;
    arr[i]->values=(int *)malloc ( 2 * sizeof ( int ));
}

but this little piece of code didn't work. How can I push structures as array values?

Upvotes: 0

Views: 38

Answers (1)

cadaniluk
cadaniluk

Reputation: 15229

"Pushing" isn't supported; neither by the standard library nor by any built-in. You'll need to write your own dynamic memory allocation mechanism instead.

Also, C doesn't support default initialization of struct members like what you are attempting to use. Use a designated initializer list.

Upvotes: 1

Related Questions