Master pointers, references, and custom data structures in C++.
Understand memory management and create custom data types in C++.
int number = 42;int* ptr = &number;int* declares a pointer to an integer&number gets the memory address of 'number' (address-of operator)cout << "Address: " << ptr << endl;cout << "Value: " << *ptr << endl;*ptr is the dereference operator - it accesses the value AT the address*ptr and number both give you 42*ptr = 100;*ptr and number will now be 100& (address-of): Gets memory address of a variable* (dereference): Accesses value at an address* in declaration: Indicates pointer type (int*, char*, etc.)int* ptr = nullptr;Value of number: 42
Address of number: 0x7fff... (memory address)
Value via pointer: 42
After modification: 100
int* ptr = &number;&variable returns the memory address*ptr gets the value at the address*ptr = newValue;Review feedback below
int original = 50;int& ref = original;int& declares a reference to an integerref becomes an alias for originalref = 75;original to 75void doubleValue(int& num) { num *= 2; }void func(int x) - receives a copyvoid func(int& x) - receives the originalOriginal value: 50
After ref = 75: 75
After doubleValue(): 150
int& ref = original;void func(int& param)ref = newValue; (no * needed)Review feedback below
struct Student {
string name;
int age;
double gpa;
};struct keyword defines a new typeStudent student1;student1.name = "Alice";student1.age = 20;student1.gpa = 3.8;. to access membersStudent student2 = {"Bob", 22, 3.5};void printStudent(Student s) { ... }void print(const Student& s)Student: Alice
Age: 20
GPA: 3.8
---
Student: Bob
Age: 22
GPA: 3.5
struct Name { type member; };instance.memberStudent s = {"Name", 20, 3.5};const Student& for efficient passingReview feedback below