0% found this document useful (0 votes)
7 views

Pointers - Comprehensive Exercise

This document outlines a comprehensive exercise focused on pointers, arrays, and dynamic memory in C++. It includes tasks such as pointer arithmetic, passing pointers to functions, dynamic memory allocation for 1D and 2D arrays, and using pointers to swap variables. The document also emphasizes the importance of memory management and includes code examples demonstrating these concepts in C++.

Uploaded by

hadytarabay12
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)
7 views

Pointers - Comprehensive Exercise

This document outlines a comprehensive exercise focused on pointers, arrays, and dynamic memory in C++. It includes tasks such as pointer arithmetic, passing pointers to functions, dynamic memory allocation for 1D and 2D arrays, and using pointers to swap variables. The document also emphasizes the importance of memory management and includes code examples demonstrating these concepts in C++.

Uploaded by

hadytarabay12
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/ 5

Comprehensive exercise: Pointers, Arrays, and Dynamic Memory in C++

Objective:
In this exercise, you will implement various features using pointers, arrays (as pointers), dynamic
memory allocation, and memory management. This will help you understand how pointers work,
how to manipulate arrays with pointers, and how to use dynamic memory effectively in C++.

Tasks:
1. Pointer Basics and Arithmetic:
• Declare an integer variable and a pointer to that variable.
• Use the pointer to modify the value of the variable and print both the pointer’s address and
the updated value.
• Create an array of integers and a pointer to its first element. Use pointer arithmetic to
traverse and print the elements of the array.
2. Pointer to Pointer:
• Declare an integer variable and a pointer pointing to its address.
• Create another pointer that stores the address of the first pointer (pointer to a pointer).
• Print the value of the integer through both levels of indirection.
3. Array as Pointers:
• Declare an array of integers and use a pointer to traverse and print the array elements.
Modify the values using pointer notation instead of array indexing, then print the updated
array.
4. Passing Pointers to Functions:
• Write a function that takes a pointer to an integer as a parameter and modifies the value of
the integer in the calling function. Call this function in main(), pass a pointer to an integer
variable, and print the value before and after the function call.
5. Dynamic Memory Allocation for 1D Array:
• Dynamically allocate memory for an array of integers using the new operator.
• Allow the user to input values into the array, print the array, and then release the allocated
memory using the delete[] operator.
6. Dynamic 2D Array Allocation:
• Dynamically allocate memory for a 2D array using pointers. Allow the user to define the
dimensions of the array and input values.
• Print the 2D array in matrix format and release the allocated memory once done.
7. Swapping Two Variables Using Pointers:
• Write a function to swap two integers using pointers. In main(), prompt the user to enter two
integers, call the function to swap them, and print the values after swapping.
8. Pointer and Const:
• Declare a pointer to a constant integer and a constant pointer to an integer.
• Try modifying the value of the variables in both cases and observe the compiler behavior.
Write comments in your code explaining why the behavior occurs.
#include <iostream>

using namespace std;

// Task 4: Function to modify integer using pointer


void modifyValue(int *p) {
*p += 10;
}

// Task 7: Function to swap two integers using pointers


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
// Task 1: Pointer basics and arithmetic
int var = 20;
int *ptr = &var;

// Modify value using pointer and print address + value


cout << "Task 1: Pointer basics and arithmetic" << endl;
cout << "Address of var: " << ptr << endl;
cout << "Value of var: " << *ptr << endl;

*ptr = 30;
cout << "Updated value of var: " << *ptr << endl;

// Array and pointer arithmetic


int arr[5] = {10, 20, 30, 40, 50};
int *arrPtr = arr; // Pointer to the first element of the array

cout << "Array elements using pointer arithmetic: ";


for (int i = 0; i < 5; i++) {
cout << *(arrPtr + i) << " ";
}
cout << endl << endl;

// Task 2: Pointer to pointer


int *ptr1 = &var;
int **ptr2 = &ptr1; // Pointer to pointer
cout << "Task 2: Pointer to Pointer" << endl;
cout << "Value of var through pointer to pointer: " << **ptr2 << endl << endl;

// Task 3: Array as pointers and modifying values


int *ptrArr = arr;
cout << "Task 3: Array elements using pointer notation: ";
for (int i = 0; i < 5; i++) {
cout << *(ptrArr + i) << " ";
}
cout << endl;

// Modify array values using pointer notation


for (int i = 0; i < 5; i++) {
*(ptrArr + i) *= 2;
}

cout << "Modified array elements: ";


for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl << endl;

// Task 4: Passing pointer to a function


int number = 50;
cout << "Task 4: Passing pointer to a function" << endl;
cout << "Original number: " << number << endl;
modifyValue(&number);
cout << "Modified number: " << number << endl << endl;

// Task 5: Dynamic memory allocation for 1D array


cout << "Task 5: Dynamic memory allocation for 1D array" << endl;
int size;
cout << "Enter the size of the dynamic array: ";
cin >> size;

int *dynArr = new int[size];


cout << "Enter " << size << " values for the dynamic array: ";
for (int i = 0; i < size; i++) {
cin >> dynArr[i];
}

cout << "Values in the dynamic array: ";


for (int i = 0; i < size; i++) {
cout << dynArr[i] << " ";
}
cout << endl;

// Deallocate memory
delete[] dynArr;

// Task 6: Dynamic 2D array allocation


cout << endl << "Task 6: Dynamic 2D array allocation" << endl;
int rows, cols;
cout << "Enter number of rows and columns for 2D array: ";
cin >> rows >> cols;

// Dynamically allocate 2D array


int **matrix = new int *[rows];
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols];
}

// Input values into 2D array


cout << "Enter values for the 2D array: " << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}

// Print the 2D array


cout << "2D array values:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

// Deallocate memory for 2D array


for (int i = 0; i < rows; i++) {
delete[] matrix[i];
}
delete[] matrix;

// Task 7: Swap two variables using pointers


cout << endl << "Task 7: Swap two variables using pointers" << endl;
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;

cout << "Before swap: a = " << a << ", b = " << b << endl;
swap(&a, &b);
cout << "After swap: a = " << a << ", b = " << b << endl;

// Task 8: Pointer and const keyword


cout << endl << "Task 8: Pointer and const" << endl;
int constNum = 100;
const int *constPtr = &constNum; // Pointer to constant int

// Uncomment the line below to see the compilation error


// *constPtr = 200; // Error: Cannot modify value through pointer to const

int varNum = 500;


int *const constPtr2 = &varNum; // Constant pointer to int
*constPtr2 = 600; // Allowed: Changing value through constant pointer
cout << "Modified varNum: " << varNum << endl;

return 0;
}

You might also like