CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2006
    Posts
    128

    Talking Double **pointer question!

    Question regarding Double pointers.

    It was told to me that if you passed a double pointer as an argument to a function :

    int function1(type arg1, (type**) &arg2)
    {
    ....
    }

    That you are passing the address to the pointer which, which is assumed is pointing to something else. And, that by doing so, you allow the function to point that double pointer to something else.

    My question is, if it is a temporary storage of the address to the pointer that the function is receiving dictated no more than by (type**), how would you re-assign it to another address. Would you have to declare a new pointer in the function and assign it the (type**) address to it, then point it to something else?

    And my last question (part II) is where does pointers really come into play and are helpful/useful?

    Thanks much

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Double **pointer question!

    arg2 = new type *[]

    pointer is an invaluable asset to efficient, robust software architecture without sacrificing resources

    Kuphryn
    Last edited by kuphryn; September 25th, 2006 at 04:39 PM.

  3. #3
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Double **pointer question!

    Quote Originally Posted by pghTech
    That you are passing the address to the pointer which, which is assumed is pointing to something else. And, that by doing so, you allow the function to point that double pointer to something else.
    Code:
    int	i = 5;
    int j = 6;
    int* pi = &i;
    
    void func(int** ppi)
    {
        // make pi point to j
        *ppi = &j;
    
        // change j to 10
        **ppi = 10;
    
        // chenge ppi. this only affects the local copy ppi
        ppi = 100;
    }
    
    int main()
    {
        //pass double pointer (address of pi)
        func(&pi);
        return 0;
    }

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Double **pointer question!

    [ Moved thread ]

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Double **pointer question!

    here is a Small mistake in the Following Code
    Quote Originally Posted by wildfrog
    Code:
    int	i = 5;
    int j = 6;
    int* pi = &i;
    
    void func(int** ppi)
    {
        // make pi point to j
        *ppi = &j;
    
        // change j to 10
        **ppi = 10;
    
        // chenge ppi. this only affects the local copy ppi
        ppi = 100;//And this Line contain Error too you are trying to convert const int to int** 
    }
    
    int main()
    {
        //pass double pointer (address of pi)
        func(&pi);
        return 0;
    }
    Am i right Peter.

    Code:
    void func(int** ppi)
    {
    	// make pi point to i
    	int my =25;
    	int *my1 =&my;
    	*ppi = &j; //assign 15 to ppi
    	*ppi =&i;//change value to ppi
    	ppi =&my1;//affect only upto local copy
    	cout<<**ppi;//will print 25  here
    	cout<<"\n";
    
    
    
    
    }
    
    int main()
    {
    	//pass double pointer (address of pi)
    	int *pi;
    	func(&pi);
    	cout<<*pi;//here it will print 10
    	return 0;
    }

    Thanx
    Last edited by humptydumpty; September 26th, 2006 at 02:00 AM.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Double **pointer question!

    Quote Originally Posted by HD
    here is a Small mistake in the Following Code
    I didn't catch the mistake you are talking about...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Double **pointer question!

    mention in bold letter
    Code:
    ppi=100;
    and here is The line.
    Code:
    ppi = 100;//And this Line contain Error too you are trying to convert const int to int** 
    Thanx

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Double **pointer question!

    A, ok. Sorry, didn't pay attention to the comments.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Double **pointer question!

    it's okay Cilu. Even that was a Minute mistake .
    Thanx

  10. #10
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Double **pointer question!

    Quote Originally Posted by pghTech
    And my last question (part II) is where does pointers really come into play and are helpful/useful?
    One....
    Heart & soul of COM.... interfaces.
    To obtain interface address from the component.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  11. #11
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Double **pointer question!

    That is.But i think when you want to perform calculation on Address of your data.that time you use pointers.

    Thanx

  12. #12
    Join Date
    Jun 2006
    Posts
    128

    Re: Double **pointer question!

    Thanks everyone for your reply's.

    I made a mistake in my original post.

    Instead of asking how "pointers" are helpful, I meant to ask

    "How are DOUBLE pointers helpful", since they are already setup to be helpful in the sense that you can point to something else, or in the case of an array of pointers, easily change the sort easily.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured