Search code examples
c++pointersaddressof

address of operator and pointer operator


What is the difference between following two function definitions?

Function declaration:

void fun(int* p);

Function Definition 1:

             void fun (int* p){
                       p += 1;
                      }

Function Definition 1:

                 void fun (*p){
                       p += 1;
                          }

Solution

  • Passing an int by pointer:

    void fun (int* p) ;
    
    void fun (int* p)
    {
        *p += 1 ; // Add 1 to the value pointed by p.
    }
    

    Passing an int by reference:

    void fun (int& p) ;
    
    void fun (int& p)
    {
        p += 1 ; // Add 1 to p.
    }