#include <iostream>
using namespace std;
int main(){
/*
//int* p,q,r; //q and r are not pointers
int* p;
int* q;
int* r;
int *p, *q, *r; //p, q and r are all pointers
*/
int a;
int *iptr;
a = 2;
iptr = &a;
cout<<a<<endl; //2
cout<<iptr<<endl; //address of iptr
*iptr = 6;
cout<<a<<endl; //6
return 0;
}
Passing by reference vs passing pointers (only C++ has pass by ref. Must use pointers in C)
#include <iostream>
using namespace std;
//passing by reference using pointers
void swap(int& x, int& y){
int temp = x;
x = y;
y = temp;
}
void swap_p(int* xp, int* yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int main(){
int a=3, b=4;
cout<<a<<" "<<b<<endl; //3 4
swap(a, b);
cout<<a<<" "<<b<<endl; //4 3
swap_p(&a, &b);
cout<<a<<" "<<b<<endl; //3 4
return 0;
}