EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  Pointers in C++

Pointers in C++ | C++ for EmSAT Achieve PDF Download

Introduction

Pointers are one of the most important and powerful features of the C++ programming language. They allow us to manipulate memory directly, which makes it possible to create dynamic data structures and to interact with hardware devices. However, pointers can also be difficult to understand for beginners. In this article, we will explain what pointers are, how they work, and how to use them in your C++ programs.

What are Pointers?

A pointer is a variable that stores the memory address of another variable. In other words, a pointer points to a memory location where some data is stored. We can use pointers to access and modify the data stored in that memory location.

How to declare a pointer variable in C++?

To declare a pointer variable, we use the * symbol. For example, to declare a pointer to an integer, we would write:

int *ptr;

This declares a pointer variable named 'ptr' that can point to an integer value. Note that the '*' symbol is used to indicate that 'ptr' is a pointer variable.

How to assign a value to a pointer variable in C++?

To assign a value to a pointer variable, we use the '&' symbol to get the address of another variable. For example, to assign the address of an integer variable named 'x' to the pointer 'ptr', we would write:

int x = 10;

int *ptr = &x;

This assigns the address of 'x' to 'ptr', which now points to the memory location where 'x' is stored.

How to access the value pointed to by a pointer in C++?

To access the value pointed to by a pointer, we use the '*' symbol again. For example, to print the value of 'x' using 'ptr', we would write:

cout << *ptr << endl;

This will print the value of 'x', which is 10.

How to modify the value pointed to by a pointer in C++?

To modify the value pointed to by a pointer, we simply assign a new value to the dereferenced pointer. For example, to change the value of 'x' using 'ptr', we would write:

*ptr = 20;

This sets the value of 'x' to 20.

Pointer Arithmetic

Pointer arithmetic allows us to perform arithmetic operations on pointers. For example, we can add or subtract an integer value from a pointer to move it to a different memory location. The size of the value we add or subtract depends on the type of pointer.
For example, let's say we have an array of integers:

int arr[] = {1, 2, 3, 4, 5};

To create a pointer to the first element of the array, we would write:

int *ptr = arr;

Now 'ptr' points to the first element of the array. We can use pointer arithmetic to access the other elements of the array. For example, to access the second element of the array, we would write:

cout << *(ptr + 1) << endl;

This will print the value 2, which is the second element of the array. Pointer Arithmetic can also be used to iterate through arrays. For example, to print all the elements of the array, we could write:

for(int i = 0; i < 5; i++) {

    cout << *(ptr + i) << " ";

}

cout << endl;

This will print all the elements of the array: 1 2 3 4 5.

Null Pointers

A null pointer is a pointerthat doesn't point to any memory location. We can create a null pointer by assigning it the value 'nullptr'. For example:

int *ptr = nullptr;

This creates a null pointer named 'ptr'.

Null pointers are often used to indicate that a pointer doesn't currently point to any valid memory location. For example, a function that searches for a value in an array might return a null pointer if the value isn't found.

Dangling Pointers

A dangling pointer is a pointer that points to a memory location that has already been deallocated or freed. Accessing the value pointed to by a dangling pointer can lead to undefined behavior, including crashes or incorrect results. To avoid dangling pointers, make sure that all pointers point to valid memory locations and are not used after the memory has been freed.

Sample Problems

1. Write a program to swap the values of two variables using pointers.

#include <iostream>

using namespace std;


void swap(int *ptr1, int *ptr2) {

    int temp = *ptr1;

    *ptr1 = *ptr2;

    *ptr2 = temp;

}


int main() {

    int x = 10, y = 20;

    cout << "Before swap: x = " << x << ", y = " << y << endl;

    swap(&x, &y);

    cout << "After swap: x = " << x << ", y = " << y << endl;

    return 0;

}

Output

Before swap: x = 10, y = 20

After swap: x = 20, y = 10

2. Write a program to find the maximum element in an array using pointers.

#include <iostream>

using namespace std;


int maxElement(int *ptr, int size) {

    int max = *ptr;

    for(int i = 1; i < size; i++) {

        if(*(ptr + i) > max) {

            max = *(ptr + i);

        }

    }

    return max;

}


int main() {

    int arr[] = {5, 10, 20, 15, 25};

    int size = sizeof(arr) / sizeof(arr[0]);

    int *ptr = arr;

    cout << "The maximum element in the array is: " << maxElement(ptr, size) << endl;

    return 0;

}

Output

The maximum element in the array is: 25

Conclusion

Pointers are an important and powerful feature of the C++ programming language. They allow us to manipulate memory directly and create dynamic data structures. However, pointers can be difficult to understand for beginners, so it's important to practice and understand how they work. With the examples and explanations provided in this article, you should have a good foundation for using pointers in your C++ programs.

The document Pointers in C++ | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

Pointers in C++ | C++ for EmSAT Achieve

,

Sample Paper

,

Exam

,

mock tests for examination

,

MCQs

,

practice quizzes

,

Pointers in C++ | C++ for EmSAT Achieve

,

ppt

,

past year papers

,

Important questions

,

Summary

,

shortcuts and tricks

,

pdf

,

Extra Questions

,

Objective type Questions

,

Previous Year Questions with Solutions

,

video lectures

,

Free

,

Viva Questions

,

Pointers in C++ | C++ for EmSAT Achieve

,

Semester Notes

;