Thread: Should i pass address of pointer or just pointer???

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    19

    Should i pass address of pointer or just pointer???

    Firstly see the code:
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    
    typedef struct temp
    {
        int data;
    } T;
     
    
    void setframe2( T **tp)
    {
        (*tp)->data = 2;
    }
    
    
    void setframe3( T *tp)
    {
        tp->data = 3;
    }
    
    
    void setframe( T *tp)
    {
        tp->data = 1;
        setframe2(&tp);
        setframe3(tp);
        
    } 
    
     
    void main()
    {
     
         T  tvar;
         tvar.data=0;
    
         cout<<"\nValue of data before call to setframe "<<tvar.data;
    
         setframe(&tvar);
    
         cout<<"\nValue of data after call to setframe "<<tvar.data;
        
        
    getch();
     
    }
    
    OUTPUT
    Value of data before call to setframe 0
    Value of data after  call to setframe 3


    In setframe i have T *tp

    when calling to setframe2, i am passing its address
    setframe2(&tp);
    and in setframe2 , tp->data has been changed
    and change is permanant as it should be.

    but when calling to setframe3, i am passing just pointer , not its address
    setframe3(tp);
    and in setframe3 , tp->data has been changed
    and change is permanant here too , why??

    why??
    i mean i am not passing address of pointer
    so why changes made are permanant, not local .

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You are changing the data pointed to by the pointer; that is why the change is permanent. It would be different if you wanted to change what the pointer points to.
    Code:
    #include <iostream>
    
    void foo(T* x)
    {
       x = new T;
       x->data = 2;
    }
    
    void bar(T** x)
    {
       *x = new T;
       (*x)->data = 2;
    }
    
    int main()
    {
       T* y = 0;
       foo(y);
       if(!y) std::cout << "NULL" << std::endl;
       else std::cout << "Non-NULL : " << y->data << std::endl;
       bar(y);
       if(!y) std::cout << "NULL" << std::endl;
       else std::cout << "Non-NULL : " << y->data << std::endl;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    The -> operator is a short way to dereference a pointer to a member of a struct. So basically:

    Code:
    someStruct* pStruct;
    pStruct->value = 1;   // Same
    (*pStruct).value = 1; // Same
    This also means that if you are passing the address of a pointer, that you are dereferencing it twice by saying (*pStruct)->value = 1; Of course, there is no reason in your case why you would need to pass the address of a pointer when you can merely pass the pointer itself. Throughtout the entire operation, when you were working with that pointer that you initially pass to setframe(), the data changes are not local, they are all dereferenced (with the '*' or '->' operators) to resolve to the initial struct you decalred in main().

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i mean i am not passing address of pointer
    so why changes made are permanant, not local .
    But arent you passing the address of the variable?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    My question is

    what is the use of pointer to pointer ,if purpose is solved by just a pointer???

    like


    when i call setframe2(&tp); i am passing address of pointer
    Code:
    void setframe2( T **tp)
    {
        (*tp)->data = 2;
    }
    and
    when i call setframe3(tp); , i am pasing just the pointer
    Code:
    void setframe3( T *tp)
    {
        tp->data = 3;
    }
    and data is being changed in both the cases.
    and result is permanant.

    sp what is the use of pointer to pointer when simple pointer solves the purpose??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    what is the use of pointer to pointer when simple pointer solves the purpose?
    None. Is this code your own, or are you asking about some example that you've seen? There are uses for pointers to pointers, but here one might simply skip the pointers and pass references instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    laserlight
    None. Is this code your own, or are you asking about some example that you've seen? There are uses for pointers to pointers, but here one might simply skip the pointers and pass references instead.
    i know there are uses of pointer to pointer, but i mean specially here where the purpose is solved by just pointer.


    my question is
    when i am calling setframe3(tp);
    and then changing the data in setframe3 by tp->data = 3;
    then this change should not be permanant.
    i mean value of data should not be 3 when accessed in main().

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i know there are uses of pointer to pointer, but i mean specially here where the purpose is solved by just pointer.
    If you're talking about this example that you've given, no. One does not even have to use a pointer (explicitly), but can pass a reference instead.

    i mean value of data should not be 3 when accessed in main().
    What makes you think so, after reading what the previous users posted?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    try to understand what i mean........

    ok, i am rephrasing my question...


    Code:
    typedef struct temp
    {
        int data;
    }T;
     
    
    void setframe( T *tp)
    {
        tp->data = 1;
    }
     
     
    void main()
    {
     
         T  *tp=new T();
         tp->data=0;
    
         cout<<"\nValue of data before call to setframe "<<tp->data;
    
         setframe(tp);
         cout<<"\nValue of data after call to setframe "<<tp->data;
    }
    its actual output is
    Value of data before call to setframe 0
    Value of data after call to setframe 1

    expected output
    Value of data before call to setframe 0
    Value of data after call to setframe 0

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you just have the wrong expectations. Let's work with simple ints:
    Code:
    #include <iostream>
    
    void setInt(int* p) {
    	*p = 1;
    }
    
    int main() {
    	int* p = new int;
    	*p = 0;
    	std::cout << "Value before setInt() call: " << *p << std::endl;
    	setInt(p);
    	std::cout << "Value after setInt() call: " << *p << std::endl;
    	delete p;
    	std::cin.get();
    	return 0;
    }
    The expected output is:
    Code:
    Value before setInt() call: 0
    Value after setInt() call: 1
    Here p is the pointer, *p is the data that is pointed to. It is clear that the *p = 1 in setInt() means "change the data pointed to by p to 1". So the data pointed to is changed to 1. As simple as that.

    For your example, tp->data = 1 is a shorthand for (*tp).data, after which the same reasoning applies: change the data member of the struct pointed to by tp to 1.

    It could be that you have the local scope of setframe() and setInt() in mind. The thing is, working with the pointer means that we're working with memory, which isnt limited by function scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    19
    i got it......................
    C/C++ IDE: Microsoft visual studio .Net 2003

    Wisdom is the reward for a lifetime of listening... when you'd have preferred to talk.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Here is an example for what a pointer to a pointer ( in C++ you one would normally use a reference to a pointer ) might be good for.

    Code:
    # include <iostream>
    using namespace std;
    
    void ex1(int * pi, int val ) { *pi = val; } 
    void ex2(int ** ppi, int val ) { *ppi = new int; **ppi= val;} 
    
    int main() {
       int a = 1;
       int * pa = &a;
       cout << "a=" << a << " *pa=" << * pa<< endl ; // both outputs are 1
       ex1( pa, 2 );
       cout << "a=" << a << " *pa=" << * pa<< endl ; // both outputs are 2
       ex2( &pa, 3 ); // changes the 
       cout << "a=" << a << " *pa=" << * pa<< endl ; // outputs are 2, 3
          // ex2 made pa point to a different location
       delete pa; // pa has to be deleted because ex2 allocated a new pointer to an int
       return 0;
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Pass by reference vs pass by pointer
    By Non@pp in forum C++ Programming
    Replies: 10
    Last Post: 02-21-2006, 01:06 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM