What Is Pointer Explain With Example in Data Structure?


Scott Campbell

Pointers are a fundamental concept in data structures that allow us to work with memory addresses directly. Understanding pointers is crucial for writing efficient and optimized code. In this article, we will explore what pointers are and provide examples to help solidify our understanding.

What is a Pointer?

A pointer is a variable that stores the memory address of another variable. It “points” to the location where the actual data is stored in memory rather than holding the data itself. Pointers provide a way to access and manipulate data indirectly.

For example:

int myNumber = 42;
int* pointerToMyNumber = &myNumber;

  • The variable myNumber holds the value 42.
  • The variable pointerToMyNumber is declared as a pointer to an integer using the asterisk (*) symbol.
  • We assign the memory address of myNumber to pointerToMyNumber using the ampersand (&) operator.

We can also access and modify the value of myNumber indirectly through pointerToMyNumber:

*pointerToMyNumber = 99;

This statement changes the value of myNumber, even though we did not directly use the variable itself. By dereferencing the pointer using an asterisk (*), we can access and modify the data it points to.

The Power of Pointers

The real power of pointers lies in their ability to dynamically allocate memory at runtime. This enables us to create data structures such as linked lists, trees, and graphs.

A common example is dynamically allocating memory for an array:

int* dynamicArray = new int[5];

This statement dynamically allocates memory for an integer array with a size of 5. The pointer dynamicArray holds the address of the first element in the allocated memory block.

We can then access and modify the elements of the array using pointer arithmetic:

*(dynamicArray + 2) = 42;

This statement assigns the value 42 to the third element of the array. By adding an offset to the pointer, we can navigate through different elements of the allocated memory block.

Null Pointers

A null pointer is a special type of pointer that does not point to any valid memory location. It is represented by the value null. Null pointers are often used to indicate that a pointer is not currently pointing to anything.

For example:

int* nullPointer = nullptr;


Pointers are a powerful tool in data structures, allowing us to work directly with memory addresses. They enable efficient memory management and provide flexibility in creating complex data structures. Understanding pointers is essential for writing efficient and optimized code.

In summary:

  • A pointer stores the memory address of another variable.
  • We can access and modify data indirectly through pointers by dereferencing them.
  • Pointers enable dynamic memory allocation at runtime.
  • Null pointers indicate that a pointer does not currently point to anything.

I hope this article has helped you understand pointers in data structures. Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy