problems with pointers and internal representation.

Hi, I've been thinking for a while and I can't understand how to write this representation:

a pointer that points to an array of pointers in which each element of that array points to a vector.

Representation:

http://i53.tinypic.com/294np83.png

All of this with dynamic memory.

this is what i did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class matriz {
      private:
              int fil,col;
              int **filaspointer; 
              int *buffer;       
              
      public:
             matriz();
             matriz(int fil,int col);
             };

matriz::matriz(int fil,int col) {
 fil=fil;
 col=col;
 filaspointer=new int[fil];
 for (int i=0; i<fil; i++){
     buffer=new int[col];
     *filaspointer[i]=&buffer;
 }
}


int main () {
    matriz test(2,3);


But I keep getting an error of cannot convert int* to int** in assigment in the line

filaspointer=new int[fil];
Before we sort out the making of the new array - we need to sort these two lines out.
1
2
 fil=fil;
 col=col;


They are legal - BUT the problem is that the function has parameters called fil and col
and the matriz class also has members called fil and col.
So which fil and col are we talking about in those two lines.

In fact we will be talking about the function parameters - so you are setting the function parameters to themselves! - NOT setting the memebr variables to the value of the function parameters.

Change either the names of the member vriables or the names of the function parameters to
avoid this problem.
Last edited on
Woops, indeed, would this problem be solved using the pointer this? As in:

1
2
this->fil=fil;
this->col=col;


Although I already changed the variable names, thanks for pointing that out ;)
Last edited on
Yes, that would work.

and this also:
1
2
    matriz::fil=fil;
    matriz::col=col;
Last edited on
Then again, how can I solve my main problem ? I NEED to implement that representation, but I don't really understand how to.
How can I make an array of pointers that within each element points to another array created dynamically?

Shouldn't the private part have a pointer to a pointer and a pointer? And I need that the pointer to a pointer points to an array of pointer that will point to another array with a row of the matrix.

But I just can't think it in my head how to do this.
As I'm a bit tired now, I'll just out with it:

1
2
3
4
5
6
7
8
9
10
11
matriz::matriz(int fil,int col) 
{
    this->fil=fil;
    this->col=col;
    
    filaspointer=new int*[fil]; 
    for (int i=0; i<fil; i++)
    {
        filaspointer[i]= new int[col];
    }
}
Thanks! The problem I had was that when i did the

filaspointer=new int*[fil];

I didn't allocate the new memory as pointer again, so that's why it wouldn't work for me, since the variable filaspointer was a pointer to a pointer, it would give me an error.

Thanks ;)
Topic archived. No new replies allowed.