-6

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;
                          }

2 Answers2

2

There's only one valid function definition, the 1st one you gave:

Function Definition 1:

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

Also you probably meant:

    (*p) += 1;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Sorry for this silly question. I was reading an article for 10 hours straight and in the end I got confused in function call with function definition. – Touseef Ahmed Awan Feb 20 '14 at 20:05
1

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.
}
jliv902
  • 1,648
  • 1
  • 12
  • 21