CodeRef
CodeRef
  • Software
    • VSCode
  • Midjourney_AI
    • Midjourney Cheat Sheet
  • aws
    • S3 Reference
    • Services
  • bash
    • .bashrc File Contents
    • CAN
    • _System Config File Locations
    • argument-length-limits
    • Conditionals
    • Data Structures
    • File Permissions
    • File Syncing
    • File System
    • Functions
    • General
    • Loops
    • My Functions
    • Networking
    • Number Operations
    • OpenVPN
    • Operators
    • Resource Management
    • Serial RS232
    • Spinning Wheel Animation
    • SSH
    • Text Operations
    • Environment Variables
  • cpp
    • ChatGPT | Pointers vs. References
    • arrays
    • Classes
    • Data Types / Structures
    • Enumerated Classes
    • Exception Handling
    • Function Objects
    • Functions
    • I/O
    • Loops
    • Macros
    • Namespaces
    • New Features
    • Pointers
    • Scope
    • Smart Pointers
    • Raw String Literals
    • Style Guide
    • Switch Case
    • Templating
    • How to Use tinyxml2
    • Useful Libraries
    • google-test
    • Conditionals
    • Rule of Three/Five
    • Optional Parameters
    • Keywords
    • Filesystem
    • Random
    • Casting
    • tools
  • git
    • Code Review Dependency Strategy
    • Git Bisect Guide
    • Git Reference
    • removing-cherry-picks
    • Useful Tools
    • Graphite Reference
  • js
    • functions
    • Javascript Reference
  • linux
    • Display
    • Dual Boot with Windows
    • File System
    • NVIDIA
    • Sending/Receiving TCP/UDP Messages in Ubuntu
    • dynamically_linked_binaries
  • markdown
    • Images
    • obsidian-reference
  • python
    • Classes
    • Exceptions
    • Functions
    • Operations
    • Python Reference
    • unittest_command-line-args
    • unittest_magicmock_GPT
    • unittest_mock
    • unittest_printing
    • unittest_subtest
    • useful-stuff
    • jupyter
    • poetry
  • ros
    • _ROS Cheat Sheet
    • Create New Workspace
    • Install ROS
    • Node Sample - Listener
    • Node Sample - Talker
    • Node Template
    • Setup
    • urdf
  • excel
    • excel-reference
  • windows
    • File System
    • WSL - Windows Subsystem for Linux
    • WSL
  • software_engineering
    • uncle_bob_lectures
      • Overview
      • Lesson 01 - Notes
  • web
    • Front End
    • Hugo
    • new_frontend_tools
  • sql
    • cheatsheet
Powered by GitBook
On this page
  • Pointers and Arrays
  • Pointers/References to Constants
  • References
  • Functions - Passing by Value vs. by Reference:
  1. cpp

Pointers

int* mypointer;
mypointer = &myvariable;
*mypointer = 23;

Pointers and Arrays

int myarray[6] = {4,8,15,16,23,42};
int* mypointer;
mypointer = myarray;
*mypointer = 10;  // Updates first element of array
mypointer++;  // Increments pointer to second element of array
*mypointer = 20;  // Updates second element of array

// Other methods to increment array pointer:
// Method 1
mypointer = &myarray[2];
*mypointer = 30;
// Method 2
mypointer = myarray + 3;
*mypointer = 40;
// Method 3
mypointer = myarray;
*(mypointer + 4) = 50;

Pointers/References to Constants

const int* pointer = &variable;
*pointer = 20;  // Won't work - constant, can't modify

// Multiple ways to declare:
int* pointer;  // non-constant pointer to non-constant integer
const int* pointer;  // non-constant pointer to constant integer
int* const pointer;  // constant pointer to non-constant integer
const int* const pointer;  // constant pointer to constant integer
const int& ref;  // non-constant reference to constant integer

References

References are different than pointers.

// The following are equivalent.
int& x = 5;
int &x = 5;

// After a reference is defined, it can be used
// exactly as a normal variable without any special
// dereferencing or syntax (like a pointer would).
// Any changes to the variable will update the same
// memory location.
x = 3;

// This function accepts as input either a normal integer
// (like `int a`) or a reference to an integer (like x above).
// Regardless of what is passed in, the function will receive
// the reference to that integer. This function will also
// return a reference to an integer.
int& func(int& input) { return 3; }
int& func(int &input) { return 3; } // equivalent

// Running the function, you treat the input and output
// like normal integers, no special syntax required.
// Just know you are receiving a reference to the same
// integer in memory used in the function.
int result = func(x);

// So basically, there's nothing special you have to do
// to use references. You just have to define the data type
// in a function's input or output as a reference data type.

Example:

int x = 5;
int& y = x;
y = 10; // This will modify the value of x to be 10.
// Both x and y refer to the same memory location.

Functions - Passing by Value vs. by Reference:

// Pass by Value:
void func(int i) { return 0; }  // Function declaration.
int num = 4;
func(num);  // Function call

// Pass by Reference:
void func(int& i) { return 0; }  // Function declaration.
void func(int &i) { return 0; }  // Function declaration (equivalent).
int num = 4;
func(num);  // Function call
  // NO DIFFERENCE IN HOW YOU PASS THE ARGUMENT BY VALUE VS. BY REFERENCE!
  // ONLY DIFFERENCE IS IN THE FUNCTION DEFINITION.

// Pass Pointer:
void func(int* i) { return 0; }  // Function declaration.
void func(int *i) { return 0; }  // Function declaration (equivalent).
int num = 4;
int* num_ptr = #
func(num_ptr);  // Function call
func(&num);  // Function call - will also work

Last updated 1 year ago