C++ Programming Labs

Master pointers, references, and custom data structures in C++.

Pointers, References & Structs - Module 4

Understand memory management and create custom data types in C++.

Lab 10: Pointers
Advanced
Coding Challenge
Your Task: Learn to work with pointers - variables that store memory addresses. Pointers are fundamental to C++ and enable powerful memory manipulation.

Detailed Requirements:
1. Declare a variable and a pointer to it:
int number = 42;
int* ptr = &number;
   • int* declares a pointer to an integer
   • &number gets the memory address of 'number' (address-of operator)
   • The pointer 'ptr' now stores where 'number' lives in memory

2. Print the memory address:
cout << "Address: " << ptr << endl;
   • Printing a pointer shows the memory address (e.g., 0x7fff5fbff8ac)
   • Each variable has a unique address in memory

3. Dereference the pointer to get the value:
cout << "Value: " << *ptr << endl;
   • *ptr is the dereference operator - it accesses the value AT the address
   • *ptr and number both give you 42

4. Modify the value through the pointer:
*ptr = 100;
   • This changes 'number' to 100 through the pointer
   • Both *ptr and number will now be 100

Key Concepts:
& (address-of): Gets memory address of a variable
* (dereference): Accesses value at an address
* in declaration: Indicates pointer type (int*, char*, etc.)
• Pointers can be null: int* ptr = nullptr;
• Always initialize pointers to avoid undefined behavior

Expected Output:
Value of number: 42 Address of number: 0x7fff... (memory address) Value via pointer: 42 After modification: 100

Requirements Checklist

Declare an int variable with a value
Declare a pointer using int* and assign the address using &
Print the memory address stored in the pointer
Dereference the pointer using * to get the value
Modify the original variable through the pointer
Print the modified value to confirm the change
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare pointer: int* ptr = &number;
• Get address: &variable returns the memory address
• Dereference: *ptr gets the value at the address
• Modify via pointer: *ptr = newValue;
• Both ptr and &number give the same address!
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 11: References
Advanced
Coding Challenge
Your Task: Learn about references - aliases to existing variables. References provide a safer, cleaner alternative to pointers for many use cases.

Detailed Requirements:
1. Create a reference to a variable:
int original = 50;
int& ref = original;
   • int& declares a reference to an integer
   • ref becomes an alias for original
   • They share the same memory location

2. Modify through the reference:
ref = 75;
   • This changes original to 75
   • No dereference operator needed (unlike pointers)

3. Create a function that uses pass-by-reference:
void doubleValue(int& num) { num *= 2; }
   • The function receives a reference, not a copy
   • Changes inside the function affect the original variable
   • More efficient than copying large objects

4. Compare pass-by-value vs pass-by-reference:
   • Pass-by-value: void func(int x) - receives a copy
   • Pass-by-reference: void func(int& x) - receives the original

Key Differences: References vs Pointers:
• References MUST be initialized; pointers can be null
• References can't be reassigned to refer to another variable
• No need to dereference; use directly like the original variable
• References are safer and cleaner for most use cases

Expected Output:
Original value: 50 After ref = 75: 75 After doubleValue(): 150

Requirements Checklist

Declare an int variable with an initial value
Create a reference using int& syntax
Modify the original through the reference
Create a function with pass-by-reference parameter
Call the function and verify the change
Print values to demonstrate reference behavior
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Declare reference: int& ref = original;
• References must be initialized at declaration
• Pass-by-reference: void func(int& param)
• Modify via reference: ref = newValue; (no * needed)
• References and original share the same memory
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 12: Structs
Advanced
Coding Challenge
Your Task: Create custom data types using structs. Structs allow you to group related variables together into a single unit.

Detailed Requirements:
1. Define a Student struct:
struct Student { string name; int age; double gpa; };
   • struct keyword defines a new type
   • Members can be different types
   • Don't forget the semicolon after the closing brace!

2. Create a struct instance and initialize it:
Student student1;
student1.name = "Alice";
student1.age = 20;
student1.gpa = 3.8;
   • Use dot notation . to access members

3. Or use initializer list:
Student student2 = {"Bob", 22, 3.5};
   • Values must be in order of declaration

4. Create a function that takes a struct parameter:
void printStudent(Student s) { ... }
   • Pass struct to functions like any other type
   • Consider using reference for efficiency: void print(const Student& s)

Key Concepts:
• Structs group related data together
• Each instance has its own copy of all members
• Structs can contain any types, including other structs
• In C++, structs can also have functions (methods)

Expected Output:
Student: Alice Age: 20 GPA: 3.8 --- Student: Bob Age: 22 GPA: 3.5

Requirements Checklist

Define a struct with at least 3 different member types
Create an instance and set values using dot notation
Create another instance using initializer list
Create a function that takes a struct as parameter
Call the function to print struct data
Access and print all struct members
Output
// Click "Run Code" to compile and execute // Your program output will appear here
Hints & Tips
• Define struct: struct Name { type member; };
• Access member: instance.member
• Initialize: Student s = {"Name", 20, 3.5};
• Don't forget the semicolon after struct definition!
• Use const Student& for efficient passing
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below