Created
April 17, 2014 06:32
-
-
Save qmaruf/10957924 to your computer and use it in GitHub Desktop.
c++, pass by ref
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pass by ref example | |
#include <iostream> | |
using namespace std; | |
/* | |
store memory address using pointer type variable. | |
int* x : x is a variable to store memory address | |
of another variable. | |
*x : use this syntax to access value from memory address. | |
int* a and *a are two completly different things. | |
*/ | |
void swap(int* a, int* b) | |
{ | |
int p = *a; | |
*a = *b; | |
*b = p; | |
} | |
int main(){ | |
int a = 10; | |
int b = 20; | |
cout<<a<<" "<<b<<endl; | |
/* | |
pass memory address using & | |
&x = memory address of variable x | |
*/ | |
swap(&a,&b); | |
cout<<a<<" "<<b<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment