__zero__

function pointers

Nov 9th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. /*
  2.         C++
  3. */
  4.  
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. void changeInt(int *v, int newVal){
  10.     /*
  11.         Change int with a function, using pointers.
  12.     */
  13.  
  14.     *v = newVal;
  15. }
  16.  
  17. int main() {
  18.     /*
  19.     First:
  20.     int x, declared with 0 value
  21.     pointer to x
  22.    
  23.     we see the x value
  24.     we change the x value to 10
  25.     we see the new x value
  26.     */
  27.     int x = 0;
  28.     int *p = &x;
  29.    
  30.     cout << x << endl;
  31.    
  32.     changeInt(p, 10);
  33.    
  34.     cout << x << endl;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment