🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

Operations on Pointers

C++ Pointer Operations

C++ Pointer Operations

In C++, pointers support various operations and manipulations. Here are some common operations on pointers:

1. Dereferencing:


int x = 10;
int* ptr = &x;

// Dereferencing the pointer
int value = *ptr;

std::cout << "Value: " << value << std::endl;

The * operator is used to dereference the pointer, i.e., obtain the value stored at the memory address it points to.

2. Pointer Arithmetic:


int numbers[] = {1, 2, 3, 4, 5};
int* ptr = numbers;

// Accessing array elements using pointer arithmetic
std::cout << "First element: " << *ptr << std::endl;  // prints 1

// Moving to the next element
ptr++;
std::cout << "Second element: " << *ptr << std::endl;  // prints 2

Pointer arithmetic involves performing arithmetic operations on pointers to navigate through memory. It's often used with arrays.

3. Increment and Decrement:


int numbers[] = {1, 2, 3, 4, 5};
int* ptr = numbers;

// Incrementing the pointer
ptr++;
std::cout << "Second element: " << *ptr << std::endl;  // prints 2

// Decrementing the pointer
ptr--;
std::cout << "First element again: " << *ptr << std::endl;  // prints 1

Incrementing and decrementing a pointer involves moving it to the next or previous memory location, based on the size of the data type it points to.

4. Comparison:


int x = 10, y = 20;
int* ptr1 = &x;
int* ptr2 = &y;

// Comparing pointers
if (ptr1 == ptr2) {
    std::cout << "Pointers point to the same location." << std::endl;
} else {
    std::cout << "Pointers point to different locations." << std::endl;
}

You can compare pointers using relational operators (==, !=, <, >, <=, >=).

5. Null Pointers:


int* ptr = nullptr; // C++11 and later
// or
int* ptr = NULL;    // pre-C++11

A null pointer does not point to any memory location. It's often used to indicate that the pointer is not currently pointing to a valid object.

6. Dynamic Memory Allocation:


int* ptr = new int; // allocate memory for an integer

// Assigning a value to the dynamically allocated memory
*ptr = 42;

// Deallocating the memory
delete ptr;

Pointers are commonly used for dynamic memory allocation using new and deallocation using delete.

7. Pointers and Functions:


void modifyValue(int* ptr) {
    *ptr = 20;
}

int main() {
    int x = 10;
    int* ptr = &x;

    modifyValue(ptr);

    std::cout << "Modified value: " << x << std::endl; // prints 20

    return 0;
}

Pointers can be used to pass variables by reference to functions.

Understanding these operations is crucial for effectively using pointers in C++ programs. However, it's important to use pointers carefully to avoid issues like dangling pointers and memory leaks.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.