5.introduction To Pointers in Data Structure
5.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?
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;
• 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”,**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,
• 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
• 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.