C Programming Tutorial

Advertisements

Pointer and Array

When we declare an array, compiler allocates continuous blocks of memoy so that all the elements of an array can be stored in that memory. The address of first allocated byte or the address of first element is assigned to an array name. Thus array name workes as pointer variable.

The address of first element is also known as base address.

Example : Displaying elements of an array using pointer.


       #include<stdio.h>

       void main()
       {
              int arr []={78,45,12,89,56,23,79,46,13,82};          // Array of 10 elements
              int *ptr;                                                                 // Pointer variable

              ptr = arr;                                                               // Assigning reference of array in
                                                                                          // pointer variable

              printf("\nValues : ");
              for(int a=1;a<=10;a++)
              {
                   printf("%d, ",*ptr);                                             // Displaying values of array
                                                                                           // using pointer
                   ptr++;                                                               // Incrementing pointer variable
               }
       }

   Output :

              Values : 78, 45, 12, 89, 56, 23, 79, 46, 13, 82,

In the above example statement 1 creates an array of 10 elements. Statement 2 creates a pointer variable ptr. As said above array name workes as pointer variable therefore statement 3 is assigning the address of array in pointer variable ptr. Now ptr have the address of first element in an array. Statement 4 will display the value at address of ptr. After display the first value, statement 5 incrrease the pointer variable ptr to point to next element in an array and statement 4 will display the next value in an array until the loop ends.

Array of Pointers

Array is a collection of values of similar type. It can also be a collection of references of similar type.

Syntax


              Data-type * array [size];
					

Example


       #include<stdio.h>

       void main()
       {
              int x=10,y=20,z=30;
              int *arr[3];                     // Declaring array of three pointer

              arr[0] = &x;                     // Assigning reference of x to array 0th position
              arr[1] = &y;                     // Assigning reference of y to array 1th position
              arr[2] = &z;                     // Assigning reference of z to array 2nd position

              printf("\nValues : ");
              for(int a=0;a<3;a++)
                   printf("%d, ",*arr[a]);

       }

   Output :

              Values : 10, 20, 30,

In the above example, we have declared three variable x, y, z and assigning the addresses of these variables into an array of pointer(*arr[]).

Related topics

Advertisement