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 us

Comments

Popular posts from this blog

Operators And Separators In C Language Operators in C Language:

Difference between asterisk and ampersand operators in c