Pointer Exercises 1
Pointer Exercises 1
1. Theory Part
▪ What is the pointer?
▪ What advantages do pointers give us?
▪ What operator allocates memory dynamically? (Give the name, example, and
return value)
▪ Why is it important to subsequently deallocate that memory?
▪ What operator deallocates memory? (Give the name, example, and the
working progress)
▪ What is the disadvantage to a dynamically allocated array?
▪ Write the code to declare an array of 10 pointers to array of 100 characters
▪ What is pointer arithmetic, and how does it differ from normal arithmetic?
(give an example)
▪ What is a segmentation fault, and how can it be caused by pointers?
▪ What happens if you try to access memory that has been deallocated?
▪ What is the significance of pointer types in pointer arithmetic? (Explain how
the type of a pointer influences arithmetic operations performed with that
pointer.)
2. Pratical Part
▪ Write a C++ function to count the number of vowels and consonants in a
string using a pointer
void countVC(char* str, int& vowels, int& consonants);
▪ Write a function that takes in an integer n and a pointer to an array of n
integers and returns a new array that contains the elements of the original
array in reverse order
int* reverseArray(int* arr, int n);
▪ Write a C++ function to concatenate two strings using pointer.
void StringConcate(char* s1, char* s2, char*& des_str);
▪ A polynomial 𝑓(𝑥) = 𝑎0 𝑥 0 + 𝑎1 𝑥1 + ⋯ + 𝑎𝑛 𝑥 𝑛 , where 𝑎𝑖 is a coefficient, is
represented by the following structure:
struct Poly {
int n; //May be changed during computation
int* a; //Should be allocated dynamically
};
Implement function:
o addPoly to perform ℎ = 𝑓 + 𝑔 :
int addPoly(Poly f, Poly g, Poly & h);
o MulPoly to perform ℎ = 𝑓 ∗ 𝑔
int mulPoly(Poly f, Poly g, Poly & h);
Note: Students must create a new matrix for the extracted submatrix.
Merely printing the submatrix to the console following a pattern is not
allowed.
▪ Write a function to display 3-d matrix in C++ with the below prototype:
int*** findSubmatrices(int** A, int rows, int cols, int p, int q, int k, int& depth)