Skip to main content

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 


Comments

Popular posts from this blog

scanf() and printf() functions

Scanf() function: It is used to read input from the keyboard. int scanf(const char *format, ...); For better understanding, lets rewrite the above as follows. scanf("format specifiers", &v1, &v2, &v3); v1, v2 and v3 are the input variables whose value needs to read from the keyboard.  We need to pass address of the variable as argument to store the input values from keyboard. Here, ampersand(&) represents address. &v1 - address of variable v1 Input value for v1 will be stored at address of variable v1 Format specifier can be any of the following %u  - unsigned int %d  - int %x  - hexadecimal %o  - octal %f  - float %lf - double %c  - char %s  - string scanf("%d", &v1); - inputs integer value scanf("%x", &v2); - inputs hexadecimal value scanf("%f", &v3); - inputs float value printf() function: It prints the given data on the output screen. printf("format specifiers", v1, v2,..vn); Here, format specifier ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Multidimensional arrays in C

Multidimensional array is also known as array of arrays.  C allows user to write arrays with one or more dimensions.  An array with more than one dimension is a multi-dimensional array.  Two dimensional array is a simplest form of multidimensional array.  Below is the general form of multidimensional array. data_type  array_name[size1][size2][size3]..[sizeN]; Consider the following example int arr[2][2][2] = {   { {1, 1}, {1, 1} },                                   { {1, 1}, {1, 1} }                               } ;   The above statement shows the declaration and initialization of multidimensional array(3d array).  Total number of elements in an array can be obtained by multiplying the size value in each dimension. int arr[2][2][2]; 2 X 2 X 2 = 8 elements in the array arr And multidim...