Advertisement
thenewboston

C Programming Tutorial - 58 - Pass by Reference vs Pass by V

Aug 23rd, 2014
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void passByValue(int i);
  5. void passByAddress(int *i);
  6.  
  7. int main()
  8. {
  9.     //up to now we have been passing a copy of that variable in
  10.     //that was called passing by value
  11.     //we can pass in the actual variable by passing the address
  12.     int tuna = 20;
  13.  
  14.     passByValue(tuna);
  15.     printf("Passing by value, tuna is now %d \n", tuna);
  16.     //not passing in a copy, but the actual variable
  17.     passByAddress(&tuna);
  18.     printf("Passing by address, tuna is now %d \n", tuna);
  19.  
  20.     return 0;
  21. }
  22.  
  23. void passByValue(int i){
  24.     i = 99;
  25.     return;
  26. }
  27.  
  28. void passByAddress(int *i){
  29.     //dereference to get the value
  30.     *i = 64;
  31.     return;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement