Thread: different between passing a pointer

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    26

    different between passing a pointer

    lets take method test which returns int

    int test(int * p);
    int test(int& p);

    please can someone explain the different between above two stateme

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What do you think are the differences between the two?
    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

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    Quote Originally Posted by laserlight View Post
    What do you think are the differences between the two?
    there is a different but i cannot understand.please someone help me with the explanation

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I'm asking you to give answering your own question a shot. Think: how would you call these two functions? If you are implementing them, how would you use the parameter if you want to access what the parameter refers to? What happens if the argument for the former is a null pointer?
    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
    Apr 2011
    Posts
    26
    below is my testing code.in here test will take a pointer as argument.but what is the different between test1 and test 2.both take integers as argument.i cant understand different between them.thank you for help me to think it out.can you explain test1 and 2?

    Code:
    #include<iostream>
    
    using namespace std;
    
    int test(int * p)
    {
        return (*p)+1;
    }
    
    int test1(int& p)
    {
        return p+1;
    }
    
    int test2(int p)
    {
        return p+1;
    }
    
    int main()
    {
        int a=1;
       cout<<test(&a)<<endl;
       cout<<test1(a)<<endl;
       cout<<test2(a)<<endl;
    
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rukshan
    what is the different between test1 and test 2.both take integers as argument.i cant understand different between them.thank you for help me to think it out.can you explain test1 and 2?
    Compile and run:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void test(int* p)
    {
        *p = *p + 1;
    }
    
    void test1(int& p)
    {
        p = p + 1;
    }
    
    void test2(int p)
    {
        p = p + 1;
    }
    
    int main()
    {
        int a = 1;
        test(&a);
        cout << a << endl;
    
        a = 1;
        test1(a);
        cout << a << endl;
    
        a = 1;
        test2(a);
        cout << a << endl;
    }
    What do you observe, and what does it tell you?
    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
    Apr 2011
    Posts
    26
    output :test - 2
    test1 - 2
    test2 -2


    Answer one is according to pointer Arithmetic .I can understand that.
    Answer 3 is according to normal integer passing.What i think is test1 and test2 are equal am i right?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you sure you compiled the code that I showed you? The output that I expect is:
    Code:
    2
    2
    1
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by rukshan View Post
    output :test - 2
    test1 - 2
    test2 -2


    Answer one is according to pointer Arithmetic .I can understand that.
    Answer 3 is according to normal integer passing.What i think is test1 and test2 are equal am i right?
    About test: It's not necessarily pointer arithmetic if a dereference is in the expression. It means that the pointee is accessed and it could just be regular arithmetic, as is the situation here. That might be pedantic of me, but pointer arithmetic is totally different, and you could make a critical mistake.

    test and test1 do the same thing, but you should notice the difference in syntax and how aliasing is different from pass by value semantics. For example, your guess at the output of the program is wrong.
    Last edited by whiteflags; 04-19-2012 at 04:32 AM. Reason: I just can't get the names right...

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    26
    Quote Originally Posted by whiteflags View Post
    About test: It's not necessarily pointer arithmetic if a dereference is in the expression. It means that the pointee is accessed and it could just be regular arithmetic, as is the situation here. That might be pedantic of me, but pointer arithmetic is totally different, and you could make a critical mistake.

    test and test1 do the same thing, but you should notice the difference in syntax and how aliasing is different from pass by value semantics. For example, your guess at the output of the program is wrong.
    still i cannot understand different between test1(int p) and test2(int& p).
    what is the different between int p and int& p which are passing as arguments

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by rukshan View Post
    still i cannot understand different between test1(int p) and test2(int& p).
    what is the different between int p and int& p which are passing as arguments
    test2(int& p); ... pass by reference. changes to p inside test2() will be reflected back to the calling function.

    test1(int p); ... pass by value, a copy of the value is passed, any changes to p inside of test1() are local, the value in the calling function will not change
    Kurt
    Last edited by ZuK; 04-19-2012 at 08:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-06-2011, 03:46 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM