22 Aug 2017

Types of Pointers in C

Pointer Definition In C Programming

pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
type *var-name;

What is a pointer in programming?

  • In computer science, a pointer is a programming language object, whose value refers to (or “points to”) another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
  • A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
  • The unary or monadic operator & gives the “address of a variable”.
  • The indirection or dereference operator gives the “contents of an object pointed to by a pointer”.
The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −
int    *ip;    /* pointer to an integer */
double *dp;    /* pointer to a double */
float  *fp;    /* pointer to a float */
char   *ch     /* pointer to a character */

What is a pointer in C programming?

Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char,int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.
Examples:
int *ptr;
int (*ptr)();
int (*ptr)[2];

Types Of Pointers In C Programming

  • NULL Pointer
  • Dangling Pointer
  • Generic Pointers
  • Wild Pointer
  • Complex Pointers
  • Near Pointer
  • Far Pointer
  • Huge Pointers

List Of Pointers In C Programming

1. Null Pointer 

  • NULL Pointer is a pointer which is pointing to nothing.
  • NULL pointer points the base address of segment.
  • In case, if you don’t have address to be assigned to pointer then you can simply use NULL
  • Pointer which is initialized with NULL value is considered as NULL pointer.
  • NULL is macro constant defined in following header files –
stdio.h
alloc.h
mem.h
stddef.h
stdlib.h

Defining NULL Value

#define NULL 0

Visual Representation

Types of Pointers in C
Below are some of the variable representations of NULL pointer.
float *ptr  = (float *)0;
char  *ptr  = (char *)0;
double *ptr = (double *)0;
char *ptr   = '\0';
int *ptr    = NULL;

Example of NULL Pointer

#include <stdio.h>
int main()
{
   int  *ptr = NULL;
   printf("The value of ptr is %u",ptr);

   return 0;
}
Output :
The value of ptr is 0

2. Dangling Pointer 

  • Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
  • In short pointer pointing to non-existing memory location is called  dangling pointer.

Examples of Dangling Pointer

There are different ways where Pointer acts as dangling pointer.

Way 1 : Using free or de-allocating memory

#include<stdlib.h>
{
    char *ptr = malloc(Constant_Value);
    .......
    .......
    .......
    free (ptr);      /* ptr now becomes a dangling pointer */
}
We have declared the character pointer in the first step. After execution of some statements we have de-allocated memory which is allocated previously for the pointer.
As soon as memory is de-allocated for pointer, pointer becomes dangling pointer

Way 2 : Out of Scope

#include<stdlib.h>
void main()
 {
   char *ptr = NULL;
   .....
   .....
   {
       char ch;
       ptr = &ch;
   } 
   .....   /* dp is now a dangling pointer */
}
  • Character Pointer is Declared in the first Step.
  • Pointer Variable ‘ptr’ is pointing to Character Variable ‘ch’ declared in the inner block .
  • As character variable is non-visible in Outer Block , then Pointer is Still Pointing to Same Invalid memory location in Outer block , then Pointer becomes “Dangling”

3. Generic Pointers

When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different times.

Example of Generic Pointer

Here is some code using a void pointer:
int
main()
{
  int i;
  char c;
  void *the_data;

  i = 6;
  c = 'a';

  the_data = &i;
  printf("the_data points to the integer value %d\n", *(int*) the_data);

  the_data = &c;
  printf("the_data now points to the character %c\n", *(char*) the_data);

  return 0;
}

4. Wild Pointer

A Pointer in C that has not been initialized till its first use is known as Wild pointer. A wild pointer points to some random memory location.

Example of Wild Pointer

int main() 
{
  int  *ptr;
  /* Ptr is a wild pointer, as it is not initialized Yet */
  printf("%d", *ptr);
}
How can we avoid Wild Pointers ?
We can initialize a pointer at the point of declaration wither by the address of some object/variable or by NULL;
int main() 
{
  int val = 5;
  int  *ptr = &val; /* Initializing pointer */
  /* Ptr is not a wild pointer, it is pointing to the address of variable val */
  printf("%d", *ptr);
}

5. Complex Pointers

Precedence : Operator precedence describes the order in which C reads expressions
Associativity : Order operators of equal precedence in an expression are applied
We need to assign the priority to the pointer declaration considering precedence and associative according to following table.
Types of Pointers in C
Where
(): This operator behaves as bracket operator or function operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator not as multiplication operator.
Identifier: It is not an operator but it is name of pointer variable. You will always find the
first priority will be assigned to the name of pointer.
Data type: It is also not an operator. Data types also includes modifier (like signed int,
long double etc.)

6. Near Pointer

  • The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.
  • 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 the help of  keyword near, we can make any pointer as near pointer.

Example of Near Pointer

#include<stdio.h>

          int main()
      {
          int x=25;
          int near* ptr;
          ptr=&x;
          printf(“%d”,sizeof ptr);
          return 0;
      }
Output: 2

7. Far Pointer

  • 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 far pointer is 4 byte or 32 bit.

Example of Far Pointer

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

Output : 4

8. Huge Pointer

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

Example of Huge Pointer

#include<stdio.h>
int main()
{
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
return 0;
}
Output : 4 4 1
Explanation:  p is huge pointer, *p is far pointer and **p is char type data variable.

Advantages of Pointers In C Programming

  • We can dynamically allocate or deallocate space in memory at run time by using pointers.
  • Using pointers we can return multiple values from a function.
  • We can pass arrays to a function as call by Reference.
  • Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of array, then we can get the address of next element by simply incrementing the pointer.
  • Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Tress etc.
  • The use of pointers results into faster execution of program.

1 comment:

commnet here