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

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

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 ...

Save image in Microsoft Paint without white background

Here, we are going to see how to save image in mspaint without white background 1. Create your own drawing or painting 2. Crop the needed portion of your drawing or painting or image. Here, I am going to crop the porting inside the red box. Now, we have cropped the portion that needs to be saved as an image. 5. Clear the screen and paste the image which we cropped earlier. 6. Decrease the screen size by 50% 7. Now, adjust your screen to fit to your image size. 8. Save the image to desired file format(jpeg, png, bmp etc) 9. Go to the saved location and open the file.  Below is the output image of mine Hope you liked this article!!  For any suggestions, please comment below.