0% found this document useful (0 votes)
43 views4 pages

Pointer Exercises and Concepts

This document provides exercises on pointers in C++. It covers built-in arrays vs pointers, pointer operators like * and &, dynamic memory allocation using new, pointer arithmetic, and common errors involving pointers. Example code segments are provided to find errors, determine output, and write code involving pointers and dynamic arrays.

Uploaded by

beshahashenafe20
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)
43 views4 pages

Pointer Exercises and Concepts

This document provides exercises on pointers in C++. It covers built-in arrays vs pointers, pointer operators like * and &, dynamic memory allocation using new, pointer arithmetic, and common errors involving pointers. Example code segments are provided to find errors, determine output, and write code involving pointers and dynamic arrays.

Uploaded by

beshahashenafe20
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
  • Exercises on Pointers

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

Common questions

Powered by AI

The output of the code snippet is: 42 42 The pointer `p1` is set to point to `v1`. When `*p1 = 42;` is executed, it changes the value of `v1` to 42, because `p1` is pointing to `v1`. The values printed by `cout << v1` and `cout << *p1` are both 42, as they refer to the same modified integer value .

When the assignment `p1 = p2;` is performed and both `p1` and `p2` are pointers to dynamic memory, `p1` will point to the same memory location as `p2`, and the original memory location pointed to by `p1` will become inaccessible, which results in a memory leak if it was dynamically allocated and not freed. It is important to first free the memory allocated to `p1` before reassigning it to avoid memory leaks .

Appending a null character ('\0') to strings in C++ is crucial because it signals the end of the string when using character arrays, allowing string-handling functions to correctly identify and process the string. Without this terminator, operations conducted by standard string functions may access unintended memory areas, leading to incorrect behaviors or program crashes .

In C++, arithmetic operations cannot be performed on void pointers because they have no size associated with them, they are used to hold any data type but do not provide enough information for the compiler to compute memory arithmetic specific to data types. This lack of size information prevents pointer arithmetic which relies on incrementing and decrementing by the data type size .

To define a type for pointer variables used to point to dynamic arrays of char type in C++, you use the typedef keyword as follows: `typedef char* CharArray;`. This declaration creates a new type name `CharArray` that can be used to declare pointers to char arrays, simplifying the code and increasing readability .

In C++, the * operator is used for both dereferencing a pointer to access the value at the pointed-to location and declaring a pointer variable. The & operator, on the other hand, is used to get the address of a variable (i.e., to create a pointer to it) and to declare reference variables. These operators facilitate memory manipulation by allowing both access to and manipulation of specific memory locations .

A pointer-based string in C++ is essentially a built-in array of chars that ends with a null character ('\0'), which marks the end of the string. This is different from a regular built-in array of chars, which may not necessarily contain a null terminator and therefore might not correctly represent a string in terms of C-style string operations .

The error in the code segment `int *x, y; x = y;` is that `y` is an uninitialized integer, but the intention seems to be to assign an integer to `x`, which is a pointer. To correct this, you should ensure that `x` is assigned the address of `y`, such as by writing `x = &y;` after properly initializing `y` .

In C++, the new operator is used to allocate memory on the heap dynamically. It initializes the memory for a specified data type and returns a pointer to the beginning of the newly allocated memory. If the allocation fails, it throws a bad_alloc exception .

Leaving a pointer uninitialized in C++ can lead to undefined behavior, as it does not point to a valid memory location. Printing the address of `number` in `cout << number << endl;` may yield a random value, leading to potential memory access violations if dereferenced. It is best practice to initialize pointers to `nullptr` or a valid address to prevent such issues .

You might also like