0

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?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Abdir
  • 87
  • 1
  • 1
  • 6

1 Answers1

1

"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.

Community
  • 1
  • 1
cadaniluk
  • 15,027
  • 2
  • 39
  • 67