C++ Pointers

In C++, a pointer is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation, array manipulation, and function parameter passing by reference.

Pointers are declared using the * symbol and can be used to directly access and modify the value stored at a specific memory address.


Syntax

1. Declaring a Pointer

The syntax to declare a pointer that stores value of specific datatype is:

</>
Copy
data_type* pointer_name;
data_type
The type of data that the pointer will point to (e.g., int, float).
pointer_name
The name of the pointer variable.

2. Assigning a Pointer

The syntax to assign the address of a variable to a pointer is:

</>
Copy
pointer_name = &variable_name;
pointer_name
The name of the pointer variable.
&variable_name
The memory address of the variable variable_name.

3. Dereferencing a Pointer

The syntax to access the value stored at a pointer is:

</>
Copy
*pointer_name
*pointer_name
Accesses the value stored at the memory address pointed to by pointer_name.

Examples

Example 1: Basic Pointer Operations

This example demonstrates how to declare, assign, and dereference a pointer.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int num = 42;          // Declare an integer variable
    int* ptr = #       // Declare and assign a pointer

    cout << "Address of num: " << ptr << endl;
    cout << "Value of num using pointer: " << *ptr << endl;

    return 0;
}

Output:

Address of num: 0x7ffee1234568
Value of num using pointer: 42

Explanation:

  1. The variable num is declared and initialized to 42.
  2. The pointer ptr is declared and assigned the memory address of num using the & operator.
  3. ptr stores the memory address of num, which is printed using cout.
  4. The value of num is accessed using the dereference operator *ptr and printed to the console.

Example 2: Pointer to an Array

This example demonstrates using a pointer to access elements of an array.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 20, 30};
    int* ptr = arr;        // Pointer points to the first element of the array

    for (int i = 0; i < 3; ++i) {
        cout << "Element " << i << ": " << *(ptr + i) << endl;
    }

    return 0;
}

Output:

Element 0: 10
Element 1: 20
Element 2: 30

Explanation:

  1. The array arr is declared and initialized with three elements: 10, 20, 30.
  2. The pointer ptr is assigned the address of the first element of arr.
  3. A loop is used to iterate over the array, and each element is accessed using *(ptr + i), where i represents the offset.
  4. The values of the array elements are printed to the console.

Key Points about Pointers

  1. Pointers store the memory address of another variable.
  2. The & operator is used to get the address of a variable.
  3. The * operator is used to dereference a pointer and access the value at the memory address it points to.
  4. Pointers are essential for dynamic memory allocation and manipulating arrays efficiently.