pointer to a pointer

This is a discussion on pointer to a pointer within the C++ Programming forums, part of the General Programming Boards category; err...guess this is a simple one for you hardcore c++ guys: how do you assign the address of a pointer ...

  1. #1
    unreg
    Guest

    pointer to a pointer

    err...guess this is a simple one for you hardcore c++ guys:
    how do you assign the address of a pointer to a another pointer..

    like:
    short *a, *b, c=2;
    a=&c;
    b=a;

    b now contains the adress to c but I wan't it contain the adress to a...
    this drives me maaaaaaaad!!!

    peace
    /unregistered

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    short *a, **b, c=2;
    a=&c;
    b=&a;
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    unreg
    Guest
    hrmm...well, maybe I'm aproahing this the wrong way...that was one of the first thing I tested but it didn't seem to work.
    this is kinda what I want to do:

    int main(){
    short*** a;
    myClass inst(a);
    ...
    }

    where a is sent to the class-constructor. The class constructor looks something like this:

    myClass:myClass(short*** tableHandle)
    {
    ...
    short* table = new short[points];
    short** tablePtr;
    tablePtr=&table;
    tableHandle=&table;
    ...
    }

    well, you get the idea...I want a to contain the adress of tablePtr in the instance of myClass which contains the adress to the dynamically allocated array, table. I've tried numerous ways to achieve this but always seem to end up with a pointon to NULL...
    AAAARGGHHHHHHH!!!!!!!!!

    thanks a bunch
    /unregistered

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's a very strange looking bunch of code there. Am I to assume that you are trying to use the class to allocate memory to the variable you're passing into the constructor?
    Code:
    int main(){
       short* a;
       myClass inst(&a);
       ...
       }
    
    myClass:myClass(short** tableHandle)
       {
       *tableHandle = new short[points];
       ...
       }
    that would be enough. But I'm confused as to why you would do this.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    unreg
    Guest
    he...no, I'm not that strange

    what I want to do is to pass a pointer to the contructor so that it points to an dynamically allocated array in the class...say I've got some kindof a table class I link with dynamically, I would like a pointer to the array containing the values of that table...

    like

    main(){
    short *** PointerFromCaller;
    myClass inst(tablePointerFromMain)
    }

    myClass::myClass(short*** PointerFromCaller){
    table = new short[512]; //<-- table is a member of the class(short *table)
    PointerFromClass=&table; //PointerFromClass is also a member(short** PointerFromClass)
    PointerFromCaller=&PointerFromClass;
    /*<---this is so that when I in the class code delete the array and create a new one PointerFromCaller always point to the table-object of the class. When I delete it and then use new aagain, the adress of table will have changed BUT PointerFromClass will be reassigned to point to the newly allocated table so that PointerFromMain also will point to that...*/
    ...//more code
    }
    is this more understandable...?
    thanks very muuuuuch!!
    /unregistered(aaaaaronnnnn)

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    int main(){
       short* a;   // a pointer to an array of shorts
       myClass inst(&a);   // pass in the address of said pointer
       ...
       }
    
    myClass:myClass(short** tableHandle) // pointer to the pointer in main
       {
       m_tablehandle = tableHandle;  //m_tablehandle is defined as short **m_tablehandle in the class
       *m_tablehandle = new short[points]; //anything you do to *m_tablehandle is done to the one in main
       ...
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm showing you how to do it but I don't think you're doing the right thing. Honestly the data should live in the class and if someone outside such as main needs it it should ask for it. A class manipulating an external piece of memory this way is a bit scary.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    It makes a lot of sense if you read code to yourself (which you should be doing to help you learn IMO) Reading a statement in C/C++ is usually done from right to left:
    Code:
    int* ip
    would be read as ip is a pointer of type int . So logically:
    Code:
    int** ipp
    would be read as (right to left) ipp is a pointer of type pointer of type int

    What I'm saying is that you understood what you wanted (a pointer to a pointer), you just needed to know how to state it; Which is logical one you see how statements can be read.

    The right to left style of reading statements will lessen frustration and make debugging much easier when you get into constant pointers and pointers to functions, just keep that in mind

  9. #9
    unreg
    Guest
    FillYourBrain: that is not at all what I'm trying to do!
    I want a pointer to dynamically allocated memory in the class, so in the call to the class-constructor I send a pointer to a pointer.
    this pointer to pointer will then point to allocated memory in the class...I don't send the pointer to the constructor to make the class allocate the memory for it! it's the other way around.
    so it's:

    myClass:myClass(short** tableHandle) // pointer to the pointer in main
    {
    m_tablehandle = new short[anynumber]; //m_tablehandle declared as short* m_tablehandle in class
    tableHandle=&m_tablehandle;// so I can acces m_tablehandle thru the pointer tableHandle in main!!!
    ...
    }

    just to clearify

    the problem is that the pointer to the pointer(short** tableHandle) in main still points to NULL and seems to remain unchanged....
    /unregistered

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's why in my example I'm passing the address of the pointer in. You have to do that to change it in main. I understand what you're trying to do. You want access to the memory outside of the class. But consider this as an alternative to passing it into the constructor:

    Code:
    class SomeClass
       {
       public:
          SomeClass()
             {
             m_ptr = new short[size];
             }
          ~SomClass()
             {
             delete [] m_ptr;
             }
          short *GetPtr()
             {
              return m_ptr;
             }
       private:
          short *m_ptr;      
       };
    
    int main(void)
       {
       SomeClass blah;
       short *ptr = blah.GetPtr();
       //do stuff with pointer.
       return 0;
       }
    this allows you to manipulate the data as you want to do. "blah" then is your source for data. Just ask it for the pointer when you need it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    or you could modify GetPtr() to better illustrate the pointer to a pointer stuff you were talking about before:
    Code:
    class SomeClass
       {
       public:
          SomeClass()
             {
             m_ptr = new short[size];
             }
          ~SomClass()
             {
             delete [] m_ptr;
             }
          void GetPtr(short **ptrptr)
             {
             *ptrptr = m_ptr;
             }
       private:
          short *m_ptr;      
       };
    
    int main(void)
       {
       SomeClass blah;
       short *ptr;
       blah.GetPtr(&ptr);
       //do stuff with pointer.
       return 0;
       }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21