Skip to content

Instantly share code, notes, and snippets.

@qmaruf
Created April 17, 2014 06:32
Show Gist options
  • Save qmaruf/10957924 to your computer and use it in GitHub Desktop.
Save qmaruf/10957924 to your computer and use it in GitHub Desktop.
c++, pass by ref
// 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