Pointer and Array in C Language - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Pointer and Array in C Language

Share This
In this tutorial, we will discuss how to access an array using a pointer.

Using a pointer variable we can hold the base address of an array. If it's a one-dimensional array, we can use a single pointer to hold its address. If the array is two-dimensional, we need to declare a pointer to the pointer to hold its base address.

In the following example, we have declared a one-dimensional array. Using a pointer variable ptr, we are holding the address of the array. Now, the variable ptr is being used to access the array.

In this example, if we perform ptr++, the pointer points to the next element of the array as the pointer holds the address of the array.

But, arr++ means we are trying to change the base address of the array, which is not possible. You can see that the statement arr++ is commented as it is not possible.

Note that the pointer variables are mutable, whereas array names are immutable.

One-dimensional Array and Pointer
#include <stdio.h> int main(void) { int arr[] = {1, 2, 5, 8, 7, 30, 15}; int *ptr; ptr = arr; //ptr points to the base address of the array printf("\n%u", arr); //base address of the array printf("\n%d", arr[3]); //element of the 3rd index of the array printf("\n%d", 3[arr]); //element of the 3rd index of the array printf("\n%u", ptr); //base address of the array printf("\n%u", &ptr); //address of ptr printf("\n%d", *ptr); //element of the 0th index of the array printf("\n%d", *(ptr + 3)); //element of the 3rd index of the array printf("\n%u", arr); //base address of the array printf("\n%u", &arr); //base address of the array printf("\n%d", *arr); //element of the 0th index of the array printf("\n%d", *(arr + 3)); //element of the 3rd index of the array printf("\n%d", ptr[4]); //element of the 4th index of the array printf("\n%d", 4[ptr]); //element of the 4th index of the array ptr++; //ptr points to the address of the 1st index of the array // arr++; printf("\n%d", *ptr); //element of the 1st index of the array printf("\n%d", *arr); //element of the 0th index of the array printf("\n\n"); return 0; }

Output


Multi-dimensional Array and Pointer
#include <stdio.h> int main(void) { int arr[10][10], i, j; for(i = 0; i < 10; i++) for(j = 0; j < 10; j++) scanf("%d", (*(arr + i) + j)); // print the element of position (2, 7) printf(" %d ", *(*(arr + 2) + 7)); return 0;
Array of Pointers
#include <stdio.h> #include <stdlib.h> int main(void) { int *arr[10], n, i, j; printf("Enter number of columns (>= 8):"); scanf("%d", &n); for(i = 0; i < n; i++) arr[i] = (int *) calloc(n, sizeof(int)); for(i = 0; i < 10; i++) for(j = 0; j < n; j++) scanf("%d", (*(arr + i) + j)); // print an element from position (2, 7) printf(" %d ", *(*(arr + 2) + 7)); return 0; }



Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.