Hi,

I need to implement a dynamic array of pointers to structures.Here's what I've done

struct node{
        int freq;
        node *lptr;
        node *rptr;
};

class M
{
        private:
        node **A;
        int length;
        public:
        M()
        {
                length=10;
                A=new node*[10];
        }
        M(int a)
        {
                length=a;
                A=new node*[a];
        }

Firstly, am I right?

Now when I use this in a function..

int i;
 for(i=0;i<length;i++)
  A[i]->freq=i;

I get Segmentation Fault.I'm pretty sure im mucking up pointers somewhere.Any help?

>Firstly, am I right?
So far, yes.

>I get Segmentation Fault.
Did you allocate memory to the individual pointer or just the array? There are two steps here, first you allocate memory for the pointers in the dynamic array, then you allocate memory to each pointer to hold a node instance.

ohhh...I did not allocate memory for each pointer to hold a node...so would that be something like...

for(i=0;i<length;i++)
A[i]=new node

???

That's it exactly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.