EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

Pointers in Data Structure

By Savi JaggaSavi Jagga

Home » Data Science » Data Science Tutorials » Data Structures Tutorial » Pointers in Data Structure

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.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

Popular Course in this category
Sale
All in One Data Science Bundle (360+ Courses, 50+ projects)360+ Online Courses | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (3,220 ratings)
Course Price

View Course

Related Courses
Oracle DBA Database Management System Training (2 Courses)SQL Training Program (7 Courses, 8+ Projects)

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

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Data Structures Tutorial
  • Basics
    • Linked List Advantages
    • What is Data Structure
    • Heap Data Structure
    • Types of Trees in Data Structure
    • AVL Tree in Data Structure
    • B Tree in Data Structure
    • B+ Tree in Data Structure
    • DFS Algorithm
    • BFS Algorithm
    • Arrays in Data Structure
    • Graph in Data Structure
    • Graph Representation
    • Breadth First Search
    • Depth Limited Search
    • Hashing in Data Structure
    • Searching in Data Structure
    • Linear Search in Data Structure
    • Linked List in Data Structure
    • Doubly linked list in Data Structure
    • Circular Linked List in Data Structure
    • Pointers in Data Structure
    • Types of Graph in Data Structure
    • Bubble Sort in Data Structure
    • Quick Sort in Data Structure
    • Bitonic Sort
    • Merge Sort in Data Structure
    • Selection Sort in Data Structure
    • Insertion Sort in Data Structure
    • Radix Sort in Data Structure
    • Stack in Data Structure
    • Queue in Data Structure
    • Priority Queue in Data Structure
    • Asymptotic Analysis
    • Tree Traversal in Data Structure
    • Tree Traversal Techniques
    • Trie Data Structure
    • Splay Tree in Data Structure
    • Spanning Tree Algorithm
    • Sparse Matrix in Data Structure
    • Radix Sort Algorithm
    • Counting Sort Algorithm
    • Skip List Data Structure
    • Linked List Algorithm
    • Linked List Types
    • Inorder Traversal of Binary Tree
    • Kruskals Algorithm
    • Prims Algorithm
    • BFS VS DFS
    • BCNF
    • Skip List
    • Hash Table?in Data Structure
    • Data Structure Interview Questions
    • Data Structures & Algorithms Interview
    • AVL Tree Deletion
    • B+ Tree Deletion
    • Decision Tree Advantages and Disadvantages
    • Data Architect Skills
    • Data Architecture Principles
    • Data Engineer Jobs
    • Data Engineer Roadmap
    • Fundamentals of Data Structure
    • Circular queue in Data Structure
    • Spanning Tree in Data Structure
    • Tree traversal types
    • Deque in Data structure
    • Shell Sort in Data Structure
    • Heap sort in data structure
    • Heap data structure C++
    • Heap data structure in Java
    • Binary Search Tree Types
    • Binary Tree in Data Structure
    • Binary Tree Types
    • Binary search tree in data structure
    • Binary Search Tree Advantages
    • Binary Search Tree Properties
    • Binary Search in Data Structure
    • Binary Tree Deletion
    • Sparse Matrix Multiplication
    • Preorder Traversal of Binary Tree
    • Postorder traversal
    • Decision Tree Hyperparameters
    • PostOrder Traversal without Recursion
    • AVL Tree Rotation
    • Avro File Format
    • Decision Tree Types
    • Binomial heap
    • Confluence Jira Integration
    • Timm Sort
    • Depth First Search

Related Courses

All in One Data Science Course

Oracle DBA Course

SQL Certification Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More