This blog is under construction

Sunday 3 November 2013

Pointer to pointer

Pointer is a variable that stores address of another variable.  Whereas, pointer to pointer is nothing but a variable that stores the address of another pointer.

Below is the declaration for pointer to pointer
      <datatype>  **<variable_name>
      char **ptr;  // pointer to pointer to char

Let us see how pointer to pointer works.  Consider the below example
       char ch = 'a';
       char *cptr, **dptr;
       cptr = &ch;  // pointer stores address  of other variable
       dptr = &cptr;  // pointer to pointer stores address of another pointer

dptr     - address of cptr
*dptr   - address of the variable ch
**dptr - value in ch which is 'a'

Pointer to pointer is also called as double pointer.  Let us see how to do memory allocation for double pointer.

Dynamic memory allocation for double pointer:
       int i, **ptr;
       ptr = (int **)malloc(sizeof(int) * 3);
      for (i = 0; i < 3; i++) {
     ptr[i] = (int *) malloc(sizeof(int) * 4);
      }

So, above set of statements is equivalent to an integer array with 3 rows and 4 columns.

Let us see a simple program on double pointer.


  #include <stdio.h>
  int main() {
        int num = 10, *ptr, **dptr;

        /* initializing pointers with valid addresses */
        ptr = &num;
        dptr = &ptr;

        /* printing outputs */
        printf("dptr  : 0x%x\t&ptr: 0x%x\n", (int)dptr, (int)&ptr);
        printf("*dptr : 0x%x\tptr : 0x%x\n", (int)*dptr, (int)ptr);
        printf("**dptr: %d\t        *ptr: %d\n", **dptr, *ptr);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  dptr  : 0xbf8d6798 &ptr: 0xbf8d6798
  *dptr : 0xbf8d679c ptr : 0xbf8d679c
  **dptr: 10                 *ptr: 10



Below program illustrates how to perform dynamic memory allocation for double pointers.


  #include <stdio.h>
  #include <stdlib.h>
  int main() {
        int i, j, row, col, **mat1, **mat2, **res;

        /* input no of rows and columns from user */
        printf("Enter the number of rows and columns:");
        scanf("%d%d", &row, &col);

        /* dynamic memory allocation for double pointers */
        mat1 = (int **)malloc(sizeof(int) * row);
        mat2 = (int **)malloc(sizeof(int) * row);
        res  = (int **)malloc(sizeof(int) * row);

        for (i = 0; i < col; i++) {
                mat1[i] = (int *)malloc(sizeof(int) * col);
                mat2[i] = (int *)malloc(sizeof(int) * col);
                res[i] = (int *)malloc(sizeof(int) * col);
        }

        /* get the input for first matrix */
        printf("Enter your inputs for matrix1:\n");
        for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                        scanf("%d", (*(mat1 +i) + j));
                }
        }

        /* get the input for second matrix */
        printf("Enter your inputs for matrix2:\n");
        for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                        scanf("%d", (*(mat2 + i) + j));
                }
        }

        /* sum of given two matrices */
        for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                        res[i][j] = mat1[i][j] + mat2[i][j];
                }
        }

        /* print the resultant matrix */
        printf("Result of addition of given two matrices:\n");
        for (i = 0; i < row; i++) {
                for (j = 0; j < col; j++) {
                        printf("%d ", res[i][j]);
                }
                printf("\n");
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of rows and columns: 3   3
  Enter your inputs for matrix 1:
  10 10 10
  20 30 40
  10 20 30
  Enter your inputs for matrix 2:
  20 20 20
  30 30 30
  40 40 40

  Result of addition of given two matrices:
  30 30 30 
  50 60 70 
  50 60 70 


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