dynamic array of pointers to structures.

N

neo-saiya-jin

I have the following code:

struct nodeType
{
string value;
int arraySize;
nodeType **child;
};

int main()
{

int i = 0;

nodeType *node = new nodeType;

cin>>node->arraySize;

node->child = new nodeType*[node->arraySize];

node->child->value = "10";

cout<<node->child->value;
}

the above is just part of my program, I'm actually trying to construct
a tree dynamically by based on the data from file the main problems
I'm encountering now is that when I make the i as the condition
variable in a for loop, so that I can dynamically create and assign
the variable, my attempt to access the data later succeed only until
the first element in the array, attempt to access the element child[1]
and onwards will result in infinite loop.
 
M

Martin York

the above is just part of my program, I'm actually trying to construct
a tree dynamically by based on the data from file the main problems
I'm encountering now is that when I make the i as the condition
variable in a for loop, so that I can dynamically create and assign
the variable, my attempt to access the data later succeed only until
the first element in the array, attempt to access the element child[1]
and onwards will result in infinite loop.

Ok. You showed us the code that works.
Can you show us the code that does not work.

Other things to think about:
You should use a constructor/destructor to allocate/deallocate the
child.
You could change child to a std::vector<nodeType*> to help with this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top