Spread the love

C++ Pointers

Pointers are a fundamental feature in C++ that provide a way to directly access and manipulate memory addresses. They are powerful but can be tricky to use correctly. Here’s an overview of how pointers work in C++:

VIDEO SOURCE:- CodeHelp  by Love Babbar

Basics of Pointers

  1. Definition: A pointer is a variable that stores the memory address of another variable.
    int *ptr; // ptr is a pointer to an integer
  2. Initialization and Assignment: You can initialize a pointer by assigning it the address of a variable using the address-of operator (&).
    int var = 5;
    int *ptr = &var; // ptr now holds the address of var
  3. Dereferencing: To access the value stored at the address a pointer is pointing to, use the dereference operator (*).
    int value = *ptr; // value is now 5, the value of var

Pointer Operations

  • Address-of Operator (&): Gives the memory address of a variable.
    int var = 5;
    int *ptr = &var; // ptr now holds the address of var
  • Dereference Operator (*): Accesses the value stored at the address the pointer points to.
    int value = *ptr; // value is now 5, the value of var
  • Pointer Arithmetic: You can perform arithmetic on pointers. For example, incrementing a pointer moves it to the next memory location of the type it points to.
    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    ptr++; // ptr now points to arr[1], the second element in the array

Common Use Cases

  1. Dynamic Memory Allocation: Pointers are used to allocate memory dynamically using new and delete.
    int *ptr = new int; // allocate memory for an int

    *ptr = 5; // assign value to the allocated memory

    delete ptr; // free the allocated memory
  2. Arrays and Pointer Arithmetic: Arrays can be accessed using pointers.
    int arr[3] = {1, 2, 3};
    int *ptr = arr;
    for (int i = 0; i < 3; i++) {
    cout << *(ptr + i) << endl; // outputs 1, 2, 3
    }
  3. Function Arguments: Pointers can be used to pass arguments to functions by reference, allowing the function to modify the original variable.
    void increment(int *ptr) {
    (*ptr)++;
    }
    int var = 5;
    increment(&var); // var is now 6

Important Concepts

  • Null Pointer: A pointer that does not point to any memory location. It is often used for initialization and error checking.
    int *ptr = nullptr; // C++11 and later
    if (ptr == nullptr) {
    // handle null pointer case
    }
  • Pointer to Pointer: A pointer that stores the address of another pointer.
    int var = 5;
    int *ptr = &var;
    int **pptr = &ptr; // pptr is a pointer to ptr
  • Const Pointers: Pointers to constant data or constant pointers.
    cpp
    const int *ptr1 = &var; // pointer to constant data (data can't be modified)

    int *const ptr2 = &var; // constant pointer (pointer can't be modified)
  • Void Pointers: Generic pointers that can point to any data type. They must be cast to another pointer type before dereferencing.
    void *ptr;
    int var = 5;
    ptr = &var;
    int *intPtr = static_cast<int*>(ptr); // cast to int pointer before dereferencing

Example Usage

Here’s a complete example demonstrating some of the above concepts:

#include <iostream>

void increment(int *ptr) {

(*ptr)++;
}
int main() {
int var = 5;
int *ptr = &var; // Pointer initialization

int **pptr = &ptr; // Pointer to pointer
std::cout << "Value of var: " << var << std::endl; // 5

std::cout << "Value pointed to by ptr: " << *ptr << std::endl; // 5

std::cout << "Value pointed to by pptr: " << **pptr << std::endl; // 5

increment(ptr); // Passing pointer to function


std::cout << "Value of var after increment: " << var << std::endl; // 6
int *dynamicPtr = new int(10); // Dynamic memory allocation

std::cout << "Value of dynamically allocated int: " << *dynamicPtr << std::endl; // 10

delete dynamicPtr; // Freeing dynamically allocated memory

return 0;

}
Categories: C++

techbloggerworld.com

Nagendra Kumar Sharma I Am Software engineer

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version