Pass a pointer into a function to modify the variable outside functions - C++ Function

C++ examples for Function:Function Parameter

Description

Pass a pointer into a function to modify the variable outside functions

Demo Code

#include <iostream>
using namespace std;
void f(int* p) {
   cout << "p = " << p << endl;
   cout << "*p = " << *p << endl;
   *p = 5;//from   ww  w  . ja v  a2s  .com
   cout << "p = " << p << endl;
}
int main() {
   int x = 47;
   cout << "x = " << x << endl;
   cout << "&x = " << &x << endl;
   f(&x);
   cout << "x = " << x << endl;
}

Result


Related Tutorials