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

Array of pointer to structure

Array of pointer to structure is nothing but an array whose elements are pointers to structure How to declare array of pointer to structure? Below is an example declaration for array of pointer to structure. struct student *arr[10]; Here, arr is an array 10 of pointer to structure student. Dynamic memory allocation: Below is the procedure to perform dynamic memory allocation for the structure pointers in an array. struct student { int age; char name[32]; }; fun() { int i = 0; struct student *arr[10]; for (i = 0; i < 10; i++) { /* dynamic memory allocation for 5 structure objects */ arr[i] = (struct student *)malloc(sizeof(struct student) * 5); } } Write a c program to pass an array of structure pointers to another function   #include <stdio.h>   #include <stdlib.h>   #include <string.h>   struct student {         int age, rollno;         char name[32];   };   /* printing details of stude...

Nested function in C

What is nested function? If a function is defined inside another function, then it is called as nested function.  Nested function example in C: Consider the following example, int add(int a, int b) {        void print(int res) {          printf("Result is %d, res);       }       print(a+b);       return 0; } Here, print() is a nested function.  Because, it is defined inside another function named add() . Example C program using nested functions: #include <stdio.h> void add(int a, int b) { void print(int res) { // nested function printf("Result is %d\n", res); return; } print(a + b); // passing sum of a and b as parameter return; } int main() { add(10, 20); return 0; }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Result is 30 Previous Next

Difference between asterisk and ampersand operators in c

Address Operator (& - Ampersand): Returns address of the given variable.  Consider the following example, int *ptr, var = 10; ptr = &var; ptr = &var sets the address of the variable var to pointer ptr.  & is also called as reference operator. Let us try to understand the purpose of reference operator using the following example program.   #include <stdio.h>   int main() {         int var = 10, *ptr;         /* assigning address of variable var to pointer ptr */         ptr = &var;         /* printing the address of the variable var */         printf("Address of var is 0x%x\n", &var);         /* printing the value of pointer ptr */         printf("Value of ptr is 0x%x\n", ptr);         return 0;   }   Output:   jp@jp-VirtualBox:~/$ ./a.out   Address of var is 0x...