Dynamic structure array inside dynamic structure?

How could I put a dynamic structure inside of a structure using only the <new> and <iostream> libraries?
This post is similar to my previous one:
http://www.cplusplus.com/forum/beginner/56039/

However the answer wasn't what I thought it was instead of getting a dynamic structure inside of a dynamic structure I just got a dynamic structure that contained a structure.

So how would this work?

#include<iostream>
using namespace std;

struct fruit{
char name[50];
double weight, volume, calories;
bool poisonous;
};

struct container{
fruit * fruits;
double weight, volume;
};


int main(){
int some_number_determined_later_in_code;
int another_number_determined_later_in_code;
container * containers = new(nothrow) containers [some_number_determined_later_in_code];
containters.fruits = new (nothrow) fruit[int another_number_determined_later_in_code;];
cout<<"Thanks for solving the problem";
system ("pause");
return 0;
}


Is the way that I've formulated it even remotely correct?

By the way this code has been typed by memory and not tested, so there's probably bugs...
If 'containers' is an array, address its elements with containers[x].fruits = ...
If containers was a single element, you'd write containers->fruits = ...
The rest seems good.
All the structures are dynamic arrays.
could someone test this?
I'm not using my own computer (it doesn't have internet) to write this.
Right now i don't have a way to transfer this code to my home pc, so i'll need someone else to test...
That is correct. Remember you have to delete them when you're done.

But you could also use std::vector instead of using dynamic arrays.
I don't understand how to use vector, so i'll just keep using struct. =)
Thanks for the link hamsterman.
When i compile a slightly fixed version of the code there i get the errors:

prog.cpp: In function ‘int main()’:
prog.cpp:21: error: expected type-specifier before ‘containers’
prog.cpp:21: error: cannot convert ‘int*’ to ‘container*’ in initialization
prog.cpp:21: error: expected ‘,’ or ‘;’ before ‘containers’
prog.cpp:22: error: ‘containters’ was not declared in this scope
prog.cpp:21: warning: unused variable ‘containers’


could someone tell me why this isn't working?
I've tried a couple of different methods.... but no luck.

new version of code:

#include<iostream>
using namespace std;
 
struct fruit{
char name[50];
double weight, volume, calories;
bool poisonous;
};
 
struct container{
fruit * fruits;
double weight, volume;
};
 
 
int main(){
int some_number_determined_later_in_code;
int another_number_determined_later_in_code;
some_number_determined_later_in_code= 3;    //just added this to get rid of an error.
another_number_determined_later_in_code=2;  //just added this to get rid of an error.
container * containers = new(nothrow) containers [some_number_determined_later_in_code];
containters.fruits = new (nothrow) fruit[another_number_determined_later_in_code];
cout<<"Thanks for solving the problem";
//system ("pause");
return 0;
}

new containers should be new container

containters.fruits should be containters->fruits

By the way, in the future, use [code] tags, instead of [output].
okay, I don't want to seem dumb, nevertheless i'm still having problems compiling it.
I want to get it correct in case someone comes back to look at this later.

#include<iostream>
using namespace std;
 
struct fruit{
char name[50];
double weight, volume, calories;
bool poisonous;
};
 
struct container{
fruit * fruits;
double weight, volume;
};
 
 
int main(){
int some_number_determined_later_in_code;
int another_number_determined_later_in_code;
some_number_determined_later_in_code= 3;    //just added this to get rid of an error.
another_number_determined_later_in_code=2;  //just added this to get rid of an error.
container * containers = new(nothrow) container [some_number_determined_later_in_code];
containters->fruits = new (nothrow) fruit[another_number_determined_later_in_code];
cout<<"Thanks for solving the problem";
//system ("pause");
return 0;
}


now gives the errors:

prog.cpp: In function ‘int main()’:
prog.cpp:22: error: ‘containters’ was not declared in this scope
prog.cpp:21: warning: unused variable ‘containers’

1
2
3
4
5
container * containers = new(nothrow) container [some_number_determined_later_in_code];
//containers is an array, you need to iterate through and new each index of containers
for(int i = 0; i< some_number_determined_later_in_code; ++i){
containters[i]->fruits = new (nothrow) fruit[another_number_determined_later_in_code];
}
You wrote "containters".
Also, naraku is (almost) right. You need a for loop here. You want to initialize all allocated elements. Though if containers is a pointer, containers[i] is an object and . should be used instead of -> to access its members.
@hamsterman
Nice catch, that's what I get for copy/pasting.
Here's the final code that works:
If you're reading this you can ignore all previous versions of code.

#include<iostream>
using namespace std;

struct fruit{
char name[50];
double weight, volume, calories;
bool poisonous;
};

struct container{
fruit * fruits;
double weight, volume;
};


int main(){
int some_number_determined_later_in_code;
int another_number_determined_later_in_code;
some_number_determined_later_in_code= 3;    //just added this to get rid of an error.
another_number_determined_later_in_code=2;  //just added this to get rid of an error.
container * containers = new(nothrow) container [some_number_determined_later_in_code];

for(int i = 0; i< some_number_determined_later_in_code; ++i){
	containers[i].fruits = new (nothrow) fruit[another_number_determined_later_in_code];
}
cout<<"Finally Done!";
system ("pause");
return 0;
}


Special Thanks to:
hamsterman
naraku9333
and
bbst

Without you the coding process would've screeched to a halt.
Topic archived. No new replies allowed.