Thread: problem in handling double pointers!!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    33

    problem in handling double pointers!!

    hello,
    I am in a problem in creating an array of dynamic memory.This is a program of 2D array which is handled by double pointers.If i have a 3*3 matrix then, for this I have three pointer for row and each row points to another three columns through another three pointers.The problem is that everything is compiling except line 12..can anyone help me please.Note that i have also assignment operator and copy constructor as well.


    Code:
    class array{
    
    private:
    
    **data; int r, int c;
    
    public:
    
    int& operator()(unsigned int i ,unsigned int j); //operator overloading
    
    array::array(unsigned int row,unsigned int col)
    
    {
        r = row;
        c = col;
        data = new int *[r];
        
        for (int i=0;i<r;i++)
        *data[i] = new int[c];  //error:cannot convert int* to int
       
    }
    
    int& array::operator()(unsigned int i,unsigned int j)    // operator overloading
    {
      
       return *(*(data + i *j));    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tried:

    Code:
    data[i] = new int[c];
    You are just dereferencing data too many times (with index and with dereference operator), so you get to the int at data[i][0].
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, in your example, 'data' is not declared properly (typo?).

    >> *data[i] = new int[c]; //error:cannot convert int* to int

    data[i] is a pointer to int, so *data[i] is an int.

    >> return *(*(data + i *j));

    That's going to put you way past the end of the array. You just need data[i][j].

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    33
    thanx

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It might be simpler to abstract out the idea of the 2D array into a class and then make an array that held pointers to instances of the class. I'm not saying it will change the functionality in any way but it will be easier to maintain and understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Another inheritance problem
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2007, 05:26 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM