Learn C++ - References - Pointers Cheatsheet - Codecademy
Learn C++ - References - Pointers Cheatsheet - Codecademy
Pass-By-Reference
In C++, pass-by-reference refers to passing parameters
to a function by using references. void swap_num(int &i, int &j) {
It allows the ability to:
int temp = i;
● Modify the value of the function arguments.
i = j;
● Avoid making copies of a variable/object for j = temp;
performance reasons.
int main() {
int a = 100;
int b = 200;
swap_num(a, b);
const Reference
In C++, pass-by-reference with const can be used
for a function where the parameter(s) won’t change int triple(int const &i) {
inside the function.
This saves the computational cost of making a copy of return i * 3;
the argument.
}
/
Memory Address
In C++, the memory address is the location in the
memory of an object. It can be accessed with the std::cout << &porcupine_count << "\n";
“address of” operator, & .
Given a variable porcupine_count , the memory
address can be retrieved by printing out
&porcupine_count . It will return something like:
0x7ffd7caa5b54 .
Pointers
In C++, a pointer variable stores the memory address of
something else. It is created using the * sign. int* pointer = &gum;
Dereference
In C++, a dereference reference operator, * , can be
used to obtain the value pointed to by a pointer variable. int gum = 3;