References and Pointers
References
int* ptr = &var;ptr holds the address to var
int x = 99;
int &y = x;
y++;
// x and y are now 100Dereference
int var1 = *ptr;var1 now holds the value of ptr not the address
nullptr alternative
#include <stdio.h>
int * ptr = NULL;
Pass by reference
int a = 1000;
int b = 2000;
void func(int &i, int &j){
i = 10;
j = 20;
}
func(a, b);In the example above the actual values of a and b will be changed to 10 and 20 respectively.
This can be more efficient, especially with const values.