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?

2
  • Please consider restricting your questions to code that will at least pass the compiler, or else to questions about why the compiler rejects the code. Yours is neither. Commented Dec 2, 2015 at 17:46
  • int arr*; won't compile. Commented Dec 2, 2015 at 18:03

1 Answer 1

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.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.