Dynamic array of pointers


Every time i try to compile this i get "base operand of '->' is not a pointer"
Any idea?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 struct nod{
    int value;
    char* name;

};
nod* add(int x,char* k){
    nod* strct=new nod;
    strct->value=x;
    strct->name=k;
    return strct;
}

int main(){
   nod** pointers=new nod*[10];
  
   for(int i=0;i<10;i++) *(pointers+i)=add(i,nullptr);
   for(int i=0;i<10;i++) cout<<(pointers+i->value)<<'\n';
  
}
Last edited on
(pointers+i->value) is not the same as (pointers+i)->value

By the way, consider using std::vector as a dynamic array instead of mucking around with pointers.
Last edited on
The error refers to pointers+i->value. The 'pointers' is (probably) a nod** and therefore the pointers+i is a nod** too.

On line 16 you do dereference nod* objects correctly with *(pointers+i).

The another issue is that you have operators + and -> obeying whatever precedence rules they happen to have. The use of line 16's syntax directly definitely leads to precedence problem: *(pointers+i)->value.

However, there is an alternative syntax for *(pointers+i) and that is pointers[i].
This seems to have different precedence:
pointers[i]->value

(One can always consult the precedence rule tables, or add enough parentheses.)
Topic archived. No new replies allowed.