This blog is under construction

Wednesday 9 October 2013

Multilevel pointers in c

A pointer can store the address of another pointer variable.  We need to do multiple dereference operation to get the original value.

Consider the below example,
int num = 100;
int *ptr, **dptr;

                                    addr: 1000
                                +-----------------+
                                |     val: 100    |
                                +-----------------+
                                    name: num

Address of num is 1000
Value of num is 100


Let us assign the address of num to ptr.
ptr = #

                                    addr: 2000
                               +-------------------+
                               |    val: 1000      |
                               +-------------------+
                                    name: ptr

Address of ptr is 2000
Value of ptr is 1000

Let us assign the address of ptr to dptr.
dptr = &ptr;

                                     addr: 3000
                              +--------------------+
                              |       val: 2000    |
                              +--------------------+
                                     name: dptr

Address of dptr is 3000
Value of dptr is 2000

In order to get the original value of num using the pointer dptr, we need to do multiple dereference.

Address of dptr is 3000 and its value is 2000.
*dptr is 1000 - If we dereference dptr(address: 3000), we will get 1000 as resultant value.
**dptr is 10 - If we dereference *dptr(address: 1000), we will get 10 as resultant value.

Here, dptr is the pointer to pointer since it stores the address(reference) of another pointer.

How many levels of indirection is allowed on a single pointer declaration?
Lower threshold is 12(minimum allowed level of indirection) and the upper threshold is implementation specific.

Below program illustrates how to dereference multilevel pointers.


  #include <stdio.h>

  int main() {
        int num = 100;
        int *ptr1, **ptr2, ***ptr3, ****ptr4, *****ptr5;

        /* assigning values to pointers */
        ptr1 = &num;
        ptr2 = &ptr1;
        ptr3 = &ptr2;
        ptr4 = &ptr3;
        ptr5 = &ptr4;

        /* printing the address and value of integer variable num */
        printf("&num: 0x%x\tnum: %d\n", (int)&num, num);

        /* printing the address and values of pointers */
        printf("&ptr1: 0x%x\tptr1: 0x%x\t", (int)&ptr1, (int)ptr1);
        printf("*ptr1    : %d\n", *ptr1);
        printf("&ptr2: 0x%x\tptr2: 0x%x\t", (int)&ptr2, (int)ptr2);
        printf("**ptr2   : %d\n", **ptr2);
        printf("&ptr3: 0x%x\tptr3: 0x%x\t", (int)&ptr3, (int)ptr3);
        printf("***ptr3  : %d\n", ***ptr3);
        printf("&ptr4: 0x%x\tptr4: 0x%x\t", (int)&ptr4, (int)ptr4);
        printf("****ptr4 : %d\n", ****ptr4);

        /* Dereferencing a multilevel pointer */
        printf("\nDereferencing multilevel pointer ptr5 step by step:\n");
        printf("ptr5      : 0x%x\n", (int)ptr5);
        printf("*ptr5     : 0x%x\n", (int)*ptr5);
        printf("**ptr5    : 0x%x\n", (int)**ptr5);
        printf("***ptr5   : 0x%x\n", (int)***ptr5);
        printf("****ptr5  : 0x%x\n", (int)****ptr5);
        printf("*****ptr5 : %d\n", *****ptr5);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  &num: 0xbfe46a0c num: 100
  &ptr1: 0xbfe46a08 ptr1: 0xbfe46a0c *ptr1    : 100
  &ptr2: 0xbfe46a04 ptr2: 0xbfe46a08 **ptr2   : 100
  &ptr3: 0xbfe46a00 ptr3: 0xbfe46a04 ***ptr3  : 100
  &ptr4: 0xbfe469fc ptr4: 0xbfe46a00 ****ptr4 : 100

  Dereferencing multilevel pointer ptr5 step by step:
  ptr5      : 0xbfe469fc
  *ptr5     : 0xbfe46a00
  **ptr5    : 0xbfe46a04
  ***ptr5   : 0xbfe46a08
  ****ptr5  : 0xbfe46a0c
  *****ptr5 : 100


1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete