Arrays in C programming



Share



Learn C array declaration, initilization, accessing array elements, using array examples, 2-dimensional arrays, passing arrays to function.

Online courses related to this article


Course description

Learn, test your C programming skills and earn certificate for software jobs



Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs



Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs

What is array in C?

An array is a collection of variables of the same type. So instead of declaring individual variables one by one all can be declared once by using an array of same data type. For example instead of declaring 5 integers separately all can be declared as an integer array and can be accessed using an index.

An integer array contains couple of integers where as a character array holds few characters inside it as array elements.

Array declaration, initialization

  • Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.
  • Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets
  • One dimensional arrays do not require the dimension to be given if the array is to be completely initialized
  • Following are some array declaration, initialization examples.

    Arr1 is an integer array holding 5 integer elements, Arr2 is a float array having 3 variables of float data type, Arr3 is a character array holding 5 characters. Note that in char carr[] = {'A','B','C','D','E'}; declaration we didn't give the number of elements the array can store. In this case compiler automatically computes the size required to store all the elements of the array in memory and allocates memory space accordingly.

    
      int Arr1[5] = {1,2,3,4,5};  /* int array holding 5 integers */
      float Arr2[3] = {0.2,3.5,4.0}; /* float array holding 3 float elements */
      char carr[] = {'A','B','C','D','E'}; /* char array holding 5 char elements */
    

    Accessing elements, addresses

  • Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name.
  • Array indices start at zero in C and go up to one less than the size of the array. For example, a five element array int Arr1[5] = {1,2,3,4,5};will have indices zero through four. This is because the index in C is actually an offset from the beginning of the array. The first element is at the beginning of the array, and hence has zero offset.
  • Use loop to access all the elements of an array
  • You can get address of element i of an array using &array[i] or array + i notation
  • The name of an array is nothing but the address of the first element of the array
  • To demonstrate the above features, let's write a small program as shown below

    
    #include "stdio.h"
    void main()
    {
      printf("=====Integer Array====\n");
      int iarr[5] = {0,1,2,3,4};
      int i;
      printf("Address of first element:%x,%x\n",iarr,&iarr[0]);
      for(i = 0;i < 5;i++)
      {
        printf("iarr %x and %x\n",&iarr[i],iarr + i);
        printf("iarr element:%d, %d\n",i,iarr[i]);
      }
      printf("=====Character Array====\n");
      char carr[5] = {'A','B','C','D','E'};
      for(i = 0;i < 5;i++)
      {
        printf("carr %x and %x\n",&carr[i],carr + i);
        printf("carr element:%d and %c\n",carr[i],carr[i]);
      }
    }
    
    Program output.
    =====================
    $ gcc prog.c
    $ ./a.out
    =====Integer Array====
    Address of first element:28fee8,28fee8
    iarr 28fee8 and 28fee8
    iarr element:0, 0
    iarr 28feec and 28feec
    iarr element:1, 1
    iarr 28fef0 and 28fef0
    iarr element:2, 2
    iarr 28fef4 and 28fef4
    iarr element:3, 3
    iarr 28fef8 and 28fef8
    iarr element:4, 4
    =====Character Array====
    carr 28fee3 and 28fee3
    carr element:65 and A
    carr 28fee4 and 28fee4
    carr element:66 and B
    carr 28fee5 and 28fee5
    carr element:67 and C
    carr 28fee6 and 28fee6
    carr element:68 and D
    carr 28fee7 and 28fee7
    carr element:69 and E
    $
    

    In above program we have tried to access elements from integer array iarr[5]. Notice that the addresses of iarr[0] is 28fee8 which is same as name of array iarr. Also notice that the address of each successive element increases by 4 which means that integer takes 4bytes of space in memory for this machine. Similarly for the character array carr addresses are incremented by 1 as each character takes one byte of space in memory.

    Passing array to a function

    We pass an array to a function by reference. Hence if we update any array element inside the function it will be reflected in the calling function as well. This is demonstrated in the following example program.

    
    #include "stdio.h"
    #define SIZE 5
    int sumArr(int iarr[], int res[]);
    void main()
    {
      int iarr[SIZE] = {0,1,2,3,4};
      int sum[2] = {0,0};
      sum[0] = sumArr(iarr,sum);
      printf("Sum is:%d,%d\n", sum[0],sum[1]);
    }
    int sumArr(int iarr[],int res[])
    {
      int i, s = 0;
      for(i = 0;i < SIZE;i++)
      {
        s += iarr[i]; 
        res[1] += iarr[i];
      }
      return s;
    }
    
    Program output.
    =====================
    $ gcc prog.c
    $ ./a.out
    Sum is:10,10
    $
    

    In the above program we calculated the sum of all numbers in iarr in two ways. One way by returning the sum from function sumArr and another way by updating the 2nd element of sum array. Notice from the program output that both outputs are same.

    To Read Full Article

    Subscribe courses or Pay Per Course & logged in

    Two dimensional arrays are nothing but an array of arrays. In the above example program ar[0] is nothing but the first array of 2-d integer array ar and ar[1] is nothing but the second array of 2-d integer array ar.

    Exercises, Solutions

    Medium
    Medium
    High
    High
    Medium
    Medium
    Low

    Want to contribute a new article? Write and upload your article information .
    Share


     Articles

     C Programming

     Linux Software

     Embedded Systems

     Python Programming

     Search Code Snippets

     Popular C search examples

     Popular C search MCQs



     Online certificate courses

     Test your skills:online tests

     C Programming

     Python Programming

     Linux Software

     Quantitative Aptitude

     Embedded System Software