Skip to main content

Pointer to an array vs Array of pointers

Difference between pointer to an array and array of pointers in c?

Pointer to an array:
Pointer to an array is also called as array pointer.

How to declare pointer to an array?
int (* ptr)[3] = NULL; /* pointer to an array of three integers */

The above declaration is the pointer to an array of 3 integers.  We need to use parenthesis to declare pointer to an array.  If we are not using parenthesis, then it would become array of pointers.  Here, the base type of pointer ptr is 3-integer array.

How to assign value to array pointer(pointer to an array)?
Consider the below example,
int arr[3][3];
ptr = arr;
"arr" is a two dimensional array with three rows and three columns.  We have assigned the address of arr[0][0](arr is equivalent to &arr[0][0]) to pointer ptr. So, pointer ptr points to the first row of the matrix "arr".  If ptr is incremented(ptr++), then it will point to the next three elements in the array "arr".  If ptr is decremented(ptr--), then it will point to the previous three elements in the array "arr".


  #include <stdio.h>
  int main() {
        int arr[3][3]; /* 3 X 3 integer array */
        int (*ptr)[3]; /* pointer to an array */

        /* assiging value to pointer */
        ptr = arr;

        /* printing addresses and values */
        printf("value of arr: 0x%x\n", arr);
        printf("value of ptr: 0x%x\n", ptr);
        printf("&arr[0][0]  : 0x%x\n", &arr[0][0]);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  value of arr: 0xbfd1b6e8
  value of ptr: 0xbfd1b6e8
  &arr[0][0]  : 0xbfd1b6e8



How to assign values to array elements using array pointers(pointer to an array)?
Consider the following examples,
     ptr[0][0] = 10;
     (*ptr)[0] = 10;
     **ptr     = 10;
All the above statements assigns value to arr[0][0](first element of the array).

     ptr[i][j]             = 20;
     (*(ptr + i))[j]     = 20;
     *(*(ptr + i) + j) = 20;
All the above statements assigns value to arr[i][j] (jth element in the ith row of the array).

How to access array elements using array pointers (pointer to an array)?
arr[0][0] is equivalent to ptr[0][0]
arr[0][0] is equivalent to (*ptr)[0]
arr[0][0] is equivalent to **ptr

arr[i][j] is equivalent to ptr[i][j]
arr[i][j] is equivalent to (*(ptr + i))[j]
arr[i][j] is equivalent to *(*(ptr + i) + j)

The below program explains how to assign values to array elements and access values from array elements using array pointers.

  #include <stdio.h>
  int main() {
        int arr[3][3]; /* 3 X 3 integer array */
        int (*ptr)[3]; /* pointer to an array */
        int i = 0, j, k = 1;

        /* assiging value to pointer */
        ptr = arr;

        /* assinging values to array elements */
        for (j = 0; j < 3; j++) {
                ptr[i][j] = k++;
        }

        i++;

        for (j = 0; j < 3; j++) {
                (*(ptr + i))[j] = k++;
        }

        i++;

        for (j = 0; j < 3; j++) {
                *(*(ptr + i) + j) = k++;
        }

        /* accessing values from array */
        printf("Values in the array arr[i][j]:\n");
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        printf("%d ", arr[i][j]);
                }
        }

        /* accessing values of the array elements using array pointers */
        printf("\nAccessing values using pointers (*(ptr + i))[j]:\n");
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        printf("%d ", (*(ptr + i))[j]);
                }
        }

        printf("\nAccessing values using pointers *(*(ptr + i) + j):\n");
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        printf("%d ", *(*(ptr + i) + j));
                }
        }

        printf("\n");

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Values in the array arr[i][j]:
  1 2 3 4 5 6 7 8 9 
  Accessing values using pointers (*(ptr + i))[j]:
  1 2 3 4 5 6 7 8 9 
  Accessing values using pointers *(*(ptr + i) + j):
  1 2 3 4 5 6 7 8 9 



How to pass array pointers(pointer to an array) as arguments to function?
If we want to pass multidimensional array/array pointer to a function, then we need to declare the function parameter as a pointer to an array.

void arrayPointer(int (*pointer)[3]);

Here, the parameter is the pointer to an array of three integers.

Dynamic memory allocation for array pointers(pointer to an array)?
         int (*ptr)[3];
         ptr = (int (*)[3]) malloc(9 * sizeof(int));

The above statement allocates memory to hold a 3 X 3 array.  Of-course, ptr is the pointer to an array of integers.

The below program explains how to pass array pointers as parameters to function and also it explains about dynamic memory allocation for array pointers.


  #include <stdio.h>
  #include <stdlib.h>

  /* function with array pointers as arguments */
  void arrayPointers(int (*arr)[3], int (*ptr)[3]) {
        int i, j, k = 1;

        /* updating values for the given array pointers */
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        ptr[i][j] = arr[i][j] = k++;
                }
        }
        return;
  }

  int main() {
        int i, j, arr[3][3], (*ptr)[3];

        /* dynamic memory allocation for array pointer */
        ptr = (int (*)[3]) malloc(sizeof(int) * 9);

        arrayPointers(arr, ptr);

        /* printing values of the array(arr) */
        printf("Values in array(arr):\n");
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        printf("%d ", arr[i][j]);
                }
        }
        printf("\n");

        /* printing the values of the array(ptr) */
        printf("Values in array(ptr):\n");
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        printf("%d ", ptr[i][j]);
                }
        }
        printf("\n");
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Values in array(arr):
  1 2 3 4 5 6 7 8 9 
  Values in array(ptr):
  1 2 3 4 5 6 7 8 9 



Array of pointers:
It is also called as pointer arrays.

How to declare array of pointers?
int *ptr[3];
Above is the declaration for an array of three integer pointers.

How to assign values to pointer arrays(array of pointers)?
int a[10], b[10], c[10];
ptr[0] = &a[0][0];
ptr[1] = &b[0][0];
ptr[2] = &c[0][0];

We have assigned the address of first elements of the arrays a, b and c to the pointers ptr[0], ptr[1] and ptr[2] correspondingly.  The above statements can also be written as below.

ptr[0] = a;
ptr[1] = b;
ptr[2] = c;

Because, 
a is equivalent to &a[0][0]
b is equivalent to &b[0][0]
c is equivalent to &c[0][0]


How to assign values to array elements using pointer arrays?
ptr[0][0] = 20;  /* assigns value to the first element of the array a */
*(ptr[1] + 2) = 20; /* assigns value to the second element of the array b */
*(*(ptr + 2) + 2) = 20; /* assigns value to the third element of the array c */

How to access array elements using pointer arrays?
a[1] is equivalent to ptr[0][1]
b[1] is equivalent to ptr[1][1]
c[1]  is equivalent to ptr[2][1]

The above can also be written as follows
a[1] is equivalent to *(ptr[0] + 1)
b[1] is equivalent to *(ptr[1] + 1)
c[1] is equivalent to *(ptr[2] + 1)

The above can also be written as follows
a[1] is equivalent to *(*ptr + 1)
b[1] is equivalent to *(*(ptr + 1) + 1)
c[1] is equivalent to *(*(ptr + 2) + 1)


  #include <stdio.h>
  int main() {
        /* array declaration + assinging values to array elements */
        int a[3] = {1, 2, 3}, b[3] = {4, 5, 6};
        int i, c[3], *ptr[3]; /* ptr is pointer to an array */

        /* assigning values to pointer array */
        ptr[0] = a;
        ptr[1] = &b[0];
        ptr[2] = c;

        /* assinging values to array elements using pointer array */
        ptr[2][0] = 7;
        *(ptr[2] + 1) = 8;
        *(*(ptr + 2) + 2) = 9;

        /* accessing array elements using pointer array */
        printf("Accessing array values using pointer array:\n");
        for (i = 0; i < 3; i++) {
                printf("%d ", ptr[0][i]);
        }

        for (i = 0; i < 3; i++) {
                printf("%d ", *(ptr[1] + i));
        }

        for (i = 0; i < 3; i++) {
                printf("%d ", *(*(ptr + 2) + i));
        }

        printf("\n");
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Accessing array values using pointer array:
  1 2 3 4 5 6 7 8 9 



How to pass pointer array(array of pointers) as arguments to function?
If we want to pass an array of pointers to a function, then we need to declare the function parameter as an array of pointers without any subscript.

void pointerArray(int *pointer[ ]);

Here, the parameter is an array of pointers.

Dynamic memory allocation for pointer arrays(array of pointers)?
         int *ptr[3];
         ptr[0] = (int *)malloc(sizeof(int) * 4);
         ptr[1] = (int *)malloc(sizeof(int) * 4);
         ptr[2] = (int *)malloc(sizeof(int) * 4);

The above statements does dynamic memory allocation.  But, each pointer(ptr[0], ptr[1], ptr[2]) points to different memory blocks.  Here, the memory block pointed by pointer ptr is not contiguous.


  #include <stdio.h>
  #include <stdlib.h>

  /* function with array of pointers as arguments */
  void arrayOfPointers(int *ptr[]) {
        int i, j, k = 1;

        /* updating ptr array with new values */
        for (i = 0; i < 3; i++) {
                for (j = 0; j < 3; j++) {
                        ptr[i][j] = k++;
                }
        }
  }

  int main() {
        int i, *ptr[3];

        /* dynamic memory allocation for array of pointers */
        for (i = 0; i < 3; i++) {
                ptr[i] = (int *) malloc(sizeof(int) * 3);
        }

        arrayOfPointers(ptr);

        /* printing the addresses and values of each element in the array(ptr) */
        printf("values & address(ptr[0]):\n");
        for (i = 0; i < 3; i++) {
                printf("    %d    0x%x\n", ptr[0][i], &ptr[0][i]);
        }

        printf("values & address(ptr[1]):\n");
        for (i = 0; i < 3; i++) {
                printf("    %d    0x%x\n", ptr[1][i], &ptr[1][i]);
        }

        printf("values & address(ptr[2]):\n");
        for (i = 0; i < 3; i++) {
                printf("    %d    0x%x\n", ptr[2][i], &ptr[2][i]);
        }

        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  values & address(ptr[0]):
    1    0x93d0008
    2    0x93d000c
    3    0x93d0010
  values & address(ptr[1]):
    4    0x93d0018
    5    0x93d001c
    6    0x93d0020
  values & address(ptr[2]):
    7    0x93d0028
    8    0x93d002c
    9    0x93d0030


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.