0% found this document useful (0 votes)
213 views2 pages

Learn C++ - References & Pointers Cheatsheet - Codecademy

This document provides an overview of references and pointers in C++. It defines a const reference as passing a parameter by reference that will not change inside the function. Pointers store the memory address of another variable. References create an alias for another object such that changes to the reference also change the original. The memory address can be accessed with the address of operator &. Dereferencing a pointer with * accesses the value at the pointer's memory address. Pass by reference allows modifying function arguments and avoids copying variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views2 pages

Learn C++ - References & Pointers Cheatsheet - Codecademy

This document provides an overview of references and pointers in C++. It defines a const reference as passing a parameter by reference that will not change inside the function. Pointers store the memory address of another variable. References create an alias for another object such that changes to the reference also change the original. The memory address can be accessed with the address of operator &. Dereferencing a pointer with * accesses the value at the pointer's memory address. Pass by reference allows modifying function arguments and avoids copying variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn C++

References & Pointers

const Reference
In C++, pass-by-reference with const can be used for int triple(int const &i) {
a function where the parameter(s) won’t change inside
the function.
This saves the computational cost of making a copy of return i * 3;
the argument.

Pointers
In C++, a pointer variable stores the memory address of int* pointer = &gum;
something else. It is created using the * sign.

References
In C++, a reference variable is an alias for another int &sonny = songqiao;
object. It is created using the & sign. Two things to
note:
1. Anything done to the reference also happens to
the original.
2. Aliases cannot be changed to alias something
else.

Memory Address
In C++, the memory address is the location in the std::cout << &porcupine_count << "\n";
memory of an object. It can be accessed with the
“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 .

Dereference
In C++, a dereference reference operator, * , can be int gum = 3;
used to obtain the value pointed to by a pointer
variable.
// * on left side is a pointer
int* pointer = &gum;

// * on right side is a dereference of


that pointer
int dereference = *pointer;

Pass-By-Reference
In C++, pass-by-reference refers to passing parameters void swap_num(int &i, int &j) {
to a function by using references.
It allows the ability to:
Modify the value of the function arguments. int temp = i;
Avoid making copies of a variable/object for i = j;
performance reasons.
j = temp;

int main() {

int a = 100;
int b = 200;

swap_num(a, b);

std::cout << "A is " << a << "\n";


std::cout << "B is " << b << "\n";

Print Share

You might also like