EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Data Structures Tutorial Pointers in Data Structure
 

Pointers in Data Structure

Savi Jagga
Article bySavi Jagga
EDUCBA
Reviewed byRavi Rathore

Updated March 24, 2023

Pointers in Data Structure

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Pointers in Data Structure

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.

Pointers in Data Structure 1

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:

C Program

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 –

  1. Steps to Create Heap Sort in C
  2. Terminologies of Graph in Data Structure
  3. Data Structure Interview Questions
  4. Examples to Implement Break Statement in C
  5. The technique of Searching in Data Structure
  6. Complete Guide to B Tree in Data Structure
  7. Learn the Types of Computer Architecture
  8. Applications of Stack in Data Structure
  9. Stack in C++ | Examples
  10. PostgreSQL Procedures | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW