1

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?

Karavana
  • 489
  • 5
  • 19
  • You have an array of values and are trying to treat those values as pointers, why would you expect this to work? What is your goal here? – ildjarn Oct 29 '12 at 23:45
  • how can I fix this, I mean I have to use this struct type and class, what to do to check if its NULL or not – Karavana Oct 29 '12 at 23:46
  • 1
    Only pointers can be null, what does it mean to compare a `mystruct` to null? – ildjarn Oct 29 '12 at 23:49
  • I am trying to make a linked list in every element of the array, I have to check if the array[i] is NULL or not, dont I ? Or am I supposed to start makin a list starting from array[i].next ? – Karavana Oct 29 '12 at 23:55
  • The only way for checking whether an array element is null to make sense is for that array to be an array of pointers, not an array of objects... Maybe you should start over with [a good book](http://stackoverflow.com/q/388242/636019). :-] – ildjarn Oct 29 '12 at 23:58

2 Answers2

2

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];
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
2

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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
shf301
  • 31,086
  • 2
  • 52
  • 86