EXERCISES ON POINTERS
1. Answer each of the following:
1.1. Built-in array names are _________________pointers.
1.2. A pointer-based string is a built-in array of chars ending with a ____character.
1.3. _____________ determines the size in bytes of a type, variable or constant at
compile time
1.4. Give two uses of the * operator and two uses of &. Name each in their
respective roles.
1.5. Describe the action of the new operator. What does the operator new return?
2. State whether each of the following is true or false. If the answer is false, explain
why.
2.1. The & operator returns a synonym for the name of the object that its operand
points to in memory.
2.2. As with array objects, use the * indirection operator to access the individual
elements of a built-in array.
2.3. Arithmetic operations cannot be performed on pointers.
3. Find the error in each of the following program segments. Assume the following
declarations and statements:
int *zPtr; // zPtr will reference built-in array z
void *sPtr = nullptr;
int number;
int z[ 6 ] = { 10, 20, 30, 40, 50, 60 };
3.1. zPtr = zPtr + 2;
3.2. // use pointer to get second value of a built-in array
zPtr = z;
number = zPtr + 2;
3.3. // assign built-in array element at 3 (the value 40) to number
zPtr = z;
number = *zPtr[ 3 ];
3.4. // display entire built-in array z in reverse order
zPtr = z;
for (int i = 6; i > 0; --i )
cout << zPtr[ i ] << endl;
3.5. // assign the value pointed to by sPtr to first element of z
z[0] = *sPtr;
3.6. z++;
4. Find the error in each of the following code segments. If the error can be corrected,
explain how.
4.1. int* number;
cout << number << endl;
4.2. double* realPtr;
long* integerPtr;
integerPtr = realPtr;
4.3. int* x, y;
x = y;
Page 1|4
EXERCISES ON POINTERS
4.4. char s[]{"this is a character array"};
for (; *s != '\0'; ++s) {
cout << *s << ' ';
}
4.5. short* numPtr, result;
void* genericPtr{numPtr};
result = *genericPtr + 7;
4.6. double x = 19.34;
double xPtr{&x};
cout << xPtr << endl;
5. What is the output if the following code fragment is embedded in a complete C++
program?
5.1.
int *p1, v1 = 0;
p1 = &v1;
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;
5.2.
int *p1, *p2;
p1 = new int;
*p1 = 42;
p2 = p1;
cout<< "*p1 = " << *p1 << endl;
cout<< "*p2 = " << *p2 << endl;
*p2 = 53;
cout<< "*p1 = " << *p1 << endl;
cout<< "*p2 = " << *p2 << endl;
p1 = new int;
*p1 = 88;
cout<< "*p1 = " << *p1 << endl;
cout<< "*p2 = " << *p2 << endl;
cout<< "Make sure you understood the logic!\n";
5.3.
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << “*p1= “<<*p1 << ", " << “*p2 =“<<*p2 << endl;
p1 = p2;
cout << “*p1= “<<*p1 << ", " << “*p2 = “<<*p2 << endl;
*p1 = 30;
cout << “*p1= “<<*p1 << ", " << “*p2 = “<<*p2 << endl;
Page 2|4
EXERCISES ON POINTERS
5.4.
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << “*p1= “<<*p1 << ", " << “*p2 =“<<*p2 << endl;
*p1 = *p2;
cout << “*p1= “<<*p1 << ", " << “*p2 =“<<*p2 << endl;
*p1 = 30;
cout << “*p1= “<<*p1 << ", " << “*p2 =“<<*p2 << endl;
5.5.
typedef int* IntPtr; //Note that to use typedef #include <cstddef> is required
int main( ) {
IntPtr p;
int arr[10];
int index;
for (index = 0; index < 10; index++)
arr[index] = index;
p = arr;
for (index = 0; index < 10; index++)
cout << p[index] << " ";
cout << endl;
for (index = 0; index < 10; index++)
p[index] = p[index] + 1;
for (index = 0; index < 10; index++)
cout << arr[index] << " ";
cout << “\nEnd”;
5.6.
int arr[10];
int *p = a;
for (int i = 0; i < 10; i++)
arr[i] = i;
for (int i = 0; i < 10; i++)
cout << p[i] << ", ";
cout << “End”;
Page 3|4
EXERCISES ON POINTERS
5.7.
int array_size = 10;
int *a;
a = new int [array_size];
int *p = a;
for (int i = 0; i < array_size; i++)
a[i] = i;
p[0] = 10;
for int (i = 0; i < array_size; i++)
cout << a[i] << ", ";
cout << “End”;
5.8.
int arraySize = 10;
int *a;
a = new int[arraySize];
for (int i = 0; i < arraySize; i++)
*(a + i) = i;
for (int i = 0; i < arraySize; i++)
cout << a[i] << ", ";
cout << “End”;
5.9.
int arraySize = 10;
int *a;
a = new int[arraySize];
for (int i = 0; i < arraySize; i++)
a[i] = i;
while (*a < 9)
{
a++;
cout << *a << ", ";
}
cout << “End”;
6. Write a type definition for pointer variables that will be used to point to
dynamic arrays. The array elements are to be of type char. Call the type
CharArray.
7. Suppose your program contains code to create a dynamic array as shown below so
that the pointer variable entry is pointing to this dynamic array. Write code to fill this
array with ten numbers typed in at the keyboard.
int *entry;
entry = new int[10];
Page 4|4