Question

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;
                          }
Était-ce utile?

La solution 2

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

Autres conseils

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top