×

Types of Pointers in C

Pointers in C

A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value. The utilization of pointers is to access the memory and also, to govern the address (manipulation of address).

A pointer is a derived datatype which is constructed with the primitive data type. The size of the pointer variable is 2-byte for any data types. The pointer variable allows us to occupy the memory runtime. Pointer variable can access the value that is outside of the function body.

For eg. If a  is a variable of type int having value \0  stored at address 3000 and address of a is assigned to some other variable b, then b  is a pointer variable pointing to memory address stored in it. In simple words, b  is a pointer variable containing the address of a(i.e 3000).

Types of Pointers in C

There are various types of pointer in C, to put a number on it, we have 8 different types of pointers in C. They are as follows

1) Void pointer  2) Null pointer  3) Wild pointer4) Dangling pointer
5) Complex pointer6) Huge pointer7) Near pointer8) Far pointer

1) Void pointer

A  pointer is said to be void when a pointer has no associated data type with it. In other words, it can point to any data type and also, it can be typecasted to any type.

Since a void pointer does not have any standard type(data type) it is also referred to as a generic pointer sometimes.

A pointer can be declared as a void pointer by simply using the keyword void.

Declaration:   void *ptr = NULL;

Example:

int main()
{
int num = 5;
void *ptr = #


printf(“%d”, *ptr);
return 0;
}

Use of Void pointer

The built functions like malloc and calloc functions (which are used for allocation of memory) returns a void pointer. Due to this reason, they can allocate a memory block for any type of data.

2) Null pointer

Another type of pointer under this classification is Null pointer. It is a special type of pointer that does not point to any memory location. In other words, If we assign a NULL value to a pointer, then the pointer is considered a Null pointer.

Note: In case of null pointer, it always contains 0 or NULL as its value.

Example:

#include<stdio.h>
int main()
{
int *ptr = NULL;
return 0;
}

Use of Null pointer

  1. Used when to initialize a pointer when that pointer is not assigned any memory address for that instance of time.
  2. It is useful for handling errors when using in-built functions like malloc function.

3) Wild pointer

A Wild pointer is a type of pointer which is used to point to some arbitrary memory location, that’s why it is referred to as Uninitialized pointers.The wild pointer is uninitialized and due to this, they  are not very efficient. The inefficiency occurs because  they may point to some unknown memory location which may cause a program to perform unintended actions or may even lead to program crash..

Example:

int main()
{
int *ptr;  //This is a wild pointer
*ptr = 5;
return 0;
}

4) Dangling pointer

A dangling pointer is another type of pointer and it is a pointer which points to some non-existing memory location.

This situation arises when a pointer is pointing at the memory address of a variable but after some time that variable is deleted from that memory location while the pointer is still pointing to it.

Example:

int main()
{
int *ptr = (int *)malloc (sizeOf(int));  
…
…
…
free(ptr);  //memory is released


return 0;
}

In the above code, when the free() function is executed, the memory assigned to ptr is released but the pointer is still pointing to the deallocated memory and hence it is called a Dangling pointer.

6) Huge pointer

A huge pointer can access memory outside the current segment and is typically of size 32 bit.

A Huge pointer is not fixed and hence the part within which a huge pointer is located can be changed .

7) Near pointer

A Near pointer is a pointer that is utilized to bit-address up to 16 bits within a given section of that computer memory which is 16 bit enabled.

Example:

#include<stdio.h>
int main()
{
   int x = 25;
   int near* ptr;
   ptr= &x;
   printf(“%d”,sizeof ptr);
   return 0;
}

8) Far pointer

A far pointer is typically 32 bit which can access memory outside that current segment. It is similar to a huge pointer but a far pointer is fixed and hence that part of that sector within which they are located cannot be changed.

Example:

#include<stdio.h>
int main()
{
  int x= 10;
  int far *ptr;
  ptr=&x;
  print(“%d”, sizeof ptr);
  return 0;
}

Conclusion

A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value. The utilization of pointers is to access the memory and also, to govern the address (manipulation of address).

Pointers in C are difficult to understand for beginners but it is an essential part of programming in C. They are various types of pointers and each type has its own benefits. It is important to learn and understand the various types in order to grab a good grip on this topic.


Related Topics

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Strcpy() in C

In C language many operations can be performed on a string. Characters in a sequence are known as string. Note:   To use this function we must include the #include<string.h> header file...

4 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Ceil and Floor in C

In arithmetic, a rational number is a number that can be expressed as the quotient p/q of two integers. Where q is zero. The set of rational numbers includes all...

6 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.