Different Types of Pointers in C

In our previous tutorials, we have seen the What are the pointers and its function. In this tutorial, we are going to see Different types of pointers in c.

Prerequisites

Before start our topic, i would recommend you to read these topics.

Different Types of Pointers in C

Pointers are the variables which is used to store the address of another variable. We can use this pointer to point address of another variable or function or another pointer. That we know already. If you dont know, please read Prerequisites.

Now we will talk about types of pointer in C. There are many types in Pointers.

  • NULL Pointer
  • Void pointer or Generic Pointers
  • Dangling Pointer
  • Wild Pointer or Bad Pointer
  • Near Pointer (old method, Not useful for now a days)
  • Far Pointer (old method, Not useful for now a days)
  • Huge Pointers (old method, Not useful for now a days)

NULL Pointer

  • NULL Pointer is a pointer which is pointing to nothing, i.e. it is assigned a null value.
  • In case, if you don’t have address to be assigned to pointer then you can simply use NULL. (It is considered a good practice to set it to null.)
  • Pointer which is initialized with NULL value is considered as a NULL pointer.
  • NULL is macro constant defined in following header files

stdio.h

alloc.h

mem.h

stddef.h

stdlib.h

  • Every pointer type i.e int *, char * each have a null pointer value.

Syntax

<data type> *<variable name> = NULL;

Example

You can check the pointer is NULL or not by using below snippet.

Note:

A null pointer is conceptually different from an uninitialized pointer. A null pointer is known not to point to any object or function; an uninitialized pointer might point anywhere. An uninitialized pointer stores an undefined value.

Void Pointer or Generic Pointers

  • A void pointer is one which does not have any data type associated with it, i.e. it can be assigned a value of any type.
  • It is a C convention for a raw address. It is capable of storing addresses of any data type.
  • This is very useful when you want a pointer to point to data of different types at different times.
  • We cannot dereference generic pointer.
  • Generic pointer can hold any type of pointers like char pointer, struct pointer, array of pointer etc without any typecasting.
  • Any type of pointer can hold generic pointer without any typecasting.
  • Generic pointers are used when we want to return such pointer which is applicable to all types of pointers. For example return type of malloc function is generic pointer because it can dynamically allocate the memory space to stores integer, float, structure etc. hence we can type cast its return type to appropriate pointer type.

Syntax

void *<data type>;

Example

Dangling Pointer

  • Dangling pointers are pointers that pointing to a memory location that has been deleted (or freed).
  • Dangling pointers arise during object destruction, when an object that has an incoming reference is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

Causes of dangling pointers

  1. Return Local Variable in Function Call

  2. Variable goes Out of Scope

  3. Accessing the De-allocating or free variable memory

Example

Return Local Variable in Function Call

Variable goes Out of Scope

Accessing the De-allocating or free variable memory

Avoiding dangling pointer errors

  • We can avoid the dangling pointer errors by initialize pointer to NULL after de-allocating memory, so that pointer will be no longer dangling.
  • Assigning NULL value means pointer is not pointing to any memory location. See below snippet to avoid dangling pointer.

Note:

In some compiler you may get warning message returning address of local variable or temporary.

Wild Pointer or Bad Pointer

  • A pointer which has not been initialized to anything till its first use (not even NULL) is known as wild pointer. The pointer may be initialized to a non-NULL garbage value that may not be a valid address.
  • Wild pointer is also called Bad pointer, because without assigning any variable address , it points to the memory location.

Note:

We can initialize a pointer at the point of declaration by the address of some object/variable or by NULL;

Old method Pointers

In TURBO C there are three types of pointers. TURBO C works under DOS operating system which is based on 8085 microprocessor. These are some old concepts used in 16 bit intel architectures in the days of MS DOS, not much useful anymore.

Near Pointers

  • Near pointer is used to store 16 bit addresses means within current segment on a 16 bit machine. The limitation is that we can only access 64kb of data at a time.
  • That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc.
  • Size of near pointer is two byte.
  • With help keyword near, we can make any pointer as near pointer.

Example

Output : 2

Far Pointers

  • The pointer which can point or access whole the residence memory of RAM, i.e., which can access all 16 segments is known as far pointer.
  • Size of the far pointer is 4 byte or 32 bit.

Example

Output : 4

In that 4bytes,

  • First 16 bit stores: Segment number
  • Next 16 bit stores: Offset address

Output: 8FD8:FFF4

Here 8FD8 is segment address and FFF4 is offset address in hexadecimal number format.

Note:

We cannot guess what will be offset address, segment address and far address of any far pointer .These address are decided by operating system.

Limitation of far pointer:

We cannot change or modify the segment address of given far address by applying any arithmetic operation on it. That is by using arithmetic operator we cannot jump from one segment to other segment. If you will increment the far address beyond the maximum value of its offset address instead of incrementing segment address it will repeat its offset address in cyclic order.

Huge Pointers

  • The pointer which can point or access whole the residence memory of RAM i.e. which can access all the 16 segments is known as huge pointer.
  • Size of huge pointer is 4 byte or 32 bit

Example

Output : 4 4 1

Normalization of huge pointer

Turbo C compiler is based on 8085 microprocessor in which physical address of memory is represented in 20 bit. Conversion of 4 byte or 32 bit huge address into 20 bit actual physical address is known as normalization.

Note:

If you will increment huge pointer it will increment both offset and segment address unlike to far pointer which only increments offset address. So if you have little knowledge about huge pointer and you are using huge pointer then you can easily access and modify the IVT, device driver memory, video memory etc. This might be dangerous for your computer.

Why there are three types of pointer in Turbo c compiler?

Answer:

Turbo c compiler is based on Dos operating system which is based on 8085 microprocessors. In 8085 microprocessor actual physical address is represented in 20 bit. But there are not any pointers which can point 20 bit address. Considering simplicity of calculations, access to actual physical address, security etc. c has introduced three type of pointer i.e. near, far and huge pointer.

You can also see the Important interview topics in C Program Here.

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!