Updated March 24, 2023
Introduction to Pointers in Data Structure
Pointers are the variables that are used to store the location of value present in the memory. A pointer to a location stores its memory address. The process of obtaining the value stored at a location being referenced by a pointer is known as dereferencing. It is the same as the index for a textbook where each page is referred by its page number present in the index. One can easily find the page using the location referred to there. Such pointers usage helps in the dynamic implementation of various data structures such as stack or list.
Why do We Need Pointers in Data Structure?
Optimization of our code and improving the time complexity of one algorithm. Using pointers helps reduce the time needed by an algorithm to copy data from one place to another. Since it used the memory locations directly, any change made to the value will be reflected at all the locations.
Example:
- Call_by_value needs the value of arguments to be copied every time any operation needs to be performed.
- Call_by_reference makes this task easier using its memory location to update the value at memory locations.
Control Program Flow: Another use of pointers is to control the program flow. This is implemented by control tables that use these pointers. These pointers are stored in a table to point to each subroutine’s entry point to be executed one after the other. These pointers reference the addresses of the various procedures. This helps while working with a recursive procedure or traversal of algorithms where there is a need to store the calling step’s location.
The next need of pointers arises in various secondary data structures such as linked lists or structures to point to the next memory locations in the list.
struct Node {
int data;
struct Node* next;
};
Example:
Dynamic Memory Allocation: Many programming languages use dynamic memory allocations to allocate the memory for run-time variables. For such type of memory, allocations heap is used rather than the stack, which uses pointers. Here pointers hold the address of these dynamically generated data blocks or array of objects. Many structured or OOPs languages use a heap or free store to provide them with storage locations. The last Node in the linked list is denoted using a NULL pointer that indicates there is no element further in the list.
How do Pointers Work in Data Structure?
Pointers are kind of variables that store the address of a variable.
Defining a Pointer
Here we discuss defining a pointer in the data structure.
Syntax:
<datatype> *variable_name
Above depicts, vaiable_name is a pointer to a variable of the specified data type.
Example:
int *ptr1 – ptr1 references to a memory location that holds data of int datatype.
int var = 30;
int *ptr1 = &var; // pointer to var
int **ptr2 = & ptr1; // pointer to pointer variable ptr1
In the above example, ‘&’ is used to denote the unary operator, which returns a variable’s address.
And ‘*’ is a unary operator that returns the value stored at an address specified by the pointer variable, thus if we need to get the value of variable referenced by a pointer, often called as dereferencing, we use:
print(“%d”, *ptr1) // prints 30
print(“%d”,**ptr2) // prints 30
We need to specify datatype- It helps to identify the number of bytes data stored in a variable; thus. Simultaneously, we increment a pointer variable, and it is incremented according to the size of this datatype only.
C Program on Pointers
Following is an example of creating pointers using C Program.
Code:
#include <stdio.h>
void pointerDemo()
{
int var1 = 30;
int *ptr1;
int **ptr2;
ptr1 = &var1;
ptr2 = &ptr1;
printf("Value at ptr1 = %p \n",ptr1);
printf("Value at var1 = %d \n",var1);
printf("Value of variable using *ptr1 = %d \n", *ptr1);
printf("Value at ptr2 = %p \n",ptr2);
printf("Value stored at *ptr2 = %d \n", *ptr2);
printf("Value of variable using **ptr2 = %d \n", **ptr2);
}
int main()
{
pointerDemo();
return 0;
}
Output:
Explanation: In the above program, we have used single and double dereferencing to display the value of the variable.
There are many types of pointers being used in computer programming:
- NULL Pointer: Such type of pointer is used to indicate that this points to an invalid object. This type of pointer is often used to represent various conditions such as the end of a list.
- VOID Pointer: This type of pointer can be used to point to the address of any type of variable, but the only limitation is that it cannot be dereferenced easily.
- WILD Pointer: It is a type of pointer which doesn’t hold the address of any variable.
- Dangling Pointer: The type of pointers that don’t refer to a valid object and are not specifically initialized to point a particular memory. For ex: int *ptr1 = malloc(sizeof(char))
- Function pointer: This is a type of pointer to reference an executable code. It is mostly used in the recursive procedure that holds the address of the code that needs to be executed later.
Disadvantage Of Pointers
It can lead to many programming errors as it allows the program to access a variable that has not been defined yet. They can be easily manipulated as the number and made to point some void locations.
Thus to avoid such a situation, many programming languages have started using constructs. Programming languages such as JAVA has replaced the concept of pointers with reference variables which can only be used to refer the address of a variable and cannot be manipulated as a number.
Conclusion
Now we can easily conclude that pointers are the references to other memory locations used for the dynamic implementation of various data structures and control its structure. The size of the pointer depends on the computer architecture. Every programming language uses pointers in one way or another such as C/C++ etc.
Recommended Articles
This is a guide to Pointers in Data Structure. Here we discuss why we need pointers in a data structure and its working and program on C pointers. You may also look at the following articles to learn more –
- Steps to Create Heap Sort in C
- Terminologies of Graph in Data Structure
- Data Structure Interview Questions
- Examples to Implement Break Statement in C
- The technique of Searching in Data Structure
- Complete Guide to B Tree in Data Structure
- Learn the Types of Computer Architecture
- Applications of Stack in Data Structure
- Stack in C++ | Examples
- PostgreSQL Procedures | Examples