Tech Study

All pointer programming exercises in C++

Pointer in C++: A pointer, is basically a variable that stores the memory address or location as its value. It points to a data type (like int or string or char) of a similar type and is generated with the * operator. The address of the variable one is working with is assigned to the pointer:

For example – 

string education = “techStudy”;  // an education variable of type string
string* ptr = &education;    // A pointer variable ptr, that stores the address of education

// Outputs the value of education (techStudy)
cout << education << “\n”;

// Outputs the memory address of education (0x6dfed4)
cout << &education << “\n”;

// Outputs the memory location of food with the pointer (0x6dfed4)
cout << ptr << “\n”;

Explanation – We created a pointer variable with the name ptr, that will point to a string variable. Keeping in my mind that the type of the pointer has to match the type of the variable we’re working with. We can use the & operator to store the memory location or address of the variable called education and assign it to the pointer. As a result, ptr will hold the value of the educations’ memory address.

Consider the following example:

  1. Write a C++ code to swap values of two variables using pointers.
#include <iostream>
 using namespace std;
//Swap function to swap 2 numbers
void swap(int *num1, int *num2)
    int temp;
    //Copy the value of num1 to some temp variable
    temp = *num1;
    //Copy the value of num2 to num1
    *num1 = *num2;
    //Copy the value of num1 stored in temp to num2
    *num2 = temp;

 int main()
    int num1, num2;
    //Inputting 2 numbers from user
    cout << "\nEnter the first number : ";
    cin >> num1;
    cout << "\nEnter the Second number : ";
    cin >> num2;
    //Passing the addresses of num1 and num2
    swap(&num1, &num2);
    //Printing the swapped values of num1 and num2
    cout << "\nFirst number : " << num1;
    cout << "\nSecond number: " << num2;
    return (0);


Enter the first number – 10

Enter the second number – 15

First number – 15

Second number – 10

List of pointer programming exercises

  1. Write C++ program to swap two numbers using pointers
  2. Write C++ program to add two numbers using pointers
  3. Write C++ program to Sum of Array Elements using Pointers
  4. Write C++ program to find length of string using pointer
  5. Write C++ program to copy one string to another string using pointer
  6. Write C++ program to concatenate two strings using pointer
  7. Write C++ program to print the elements of the array in reverse order using a pointer

TaggedAll pointer programming exercises in C++

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more