I want to create an array of linked structs, but I don't know how to populate such array. Here is an example of what I want to do.
struct foo {
    int data;
    foo* next;
};
I want to declare the array inside a loop
while(1) {
    foo array[n];
    // init array, data to -1 and next to NULL;
I'd like to put things inside of it, creating new instances of foo in a way that all foo's linked in index i share a common property.
    foo* new_foo = new foo;
    new_foo -> data = x;
    new_foo -> next = array + i; // index
    array[i] = *new_foo;
    //do things
    iterate(array);
    //delete[] array; maybe
} // end loop, start again with a new array.
The iterate method would be something like this.
for(int i=0; i<n; ++i) {
    foo* iter = array + i;
    while(iter != NULL) {
        //do things
        iter = iter -> next;
    }
}
It doesn't work at all, the iterate method goes on an infinite loop. The error could be somewhere else, but I still don't know if this is the proper way of doing it. I know I have to use delete somewhere too. I'm still new to c++ and I'd love any advice from you. Thanks!
Edit:
This works fine, if anyone wonders.
foo* array[n] = {NULL};
foo* new_foo = new foo;
new_foo -> data = x;
new_foo -> next = array[i];
array[i] = new_foo;
 
    