0% found this document useful (0 votes)
29 views

5.introduction To Pointers in Data Structure

Uploaded by

Abdullah Opadeji
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

5.introduction To Pointers in Data Structure

Uploaded by

Abdullah Opadeji
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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.
Why do We Need Pointers in Data
Structure?...
• 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.
• Any variable we deal with has memory location(address) to store its
value, and the pointer is something like a variable that holds that
memory address of the variable.
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.
so if we have a pointer “p” to points to the address of a variable “a”.
p=&a; //the variable p stores the address of variable a
and if we want to access the value of the variable “a” using the
address, this will be as the following:-
*(&a) or *p
the strike (*) is used to access the value found in the address of the
variable (a).

The next picture can make the idea of pointer more clear to you.
• In C language, when we declare a pointer, it follows the following
format:-
type *var-name;
where:-
• type is the type of data accessed by the pointer.
• var_name is the name of the pointer
the strike (*) means we will access the value that the address stored in
the pointer has.
for example:
• int a = 10;
• int *p;

• p=&a; //p holds the address of a

• // *p will equal to *(&a) which equals (10)


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
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.

You might also like