Karavana
Karavana

Reputation: 487

Array of structs seems like not want to work

I have this class and struct, and every struct on the array connected to another struct (linked list structure on every element of array).. btw this is just a sample of my code, assume there are no other errors..

class DATA;
class myclass
{
     public:
         myclass();

         mystruct* addDATA(DATA *d);

     private:
         mystruct* array;
};
struct mystruct
{
    DATA     * data;
    mystruct * next ;

};

In the constructor I am trying this

 mystruct::mystruct()
 {
     mystruct* array = new mystruct[10];
     for(int i = 0; i < 10; i++)
         array[i] = NULL; 
 }

this gives an unexpected(at least to me) error, Isn't this error a bit ridiculous, I am making pointers to point to NULL.

no match for ‘operator=’ in ‘*(array + ((unsigned int)(((unsigned int)i) * 8u))) = 0’

And also when I try to;

while(this->array[i] != NULL){
    // do some arrangements on the array..

}

This also gives the same error.. Why would matching a pointer to NULL give this error, or checking if its NULL or not..Struggling since this morning, couldn't find a **ing solution :/ Array's type is "mystruct" but nothing else, Is this a problem? Or array[i] is not a pointer or something?

Upvotes: 1

Views: 101

Answers (2)

shf301
shf301

Reputation: 31394

No you are not making an array of pointers, you're a making an array of structures. Each element of array is a mystruct not a mystruct*. It doesn't make any sense to set a mystruct to null.

Upvotes: 2

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

You have an array of mystructs, not array of pointers to mystruct. If you really want an array of pointers, allocate it like this:

mystruct** array = new mystruct*[10];

Upvotes: 2

Related Questions