C Programming Tutorial – C Arrays

In C programming language, an array is a data structure. Before we discuss C arrays, let us understand the reasons and motivation for creating an array.

Suppose you want to create 10 variable of integer type, there is no problem in creating, accessing or modifying these variables. Also it is very easy to use them in a C program.

However, if you want to create 1000 variables of the same type and keep the values in some ordered fashion – ascending or descending order, think about the amount of work you need to do to maintain them.

Some of the difficulties are

  • Ordering the variables based on their values.
  • Accessing a variable means you should remember the name of the variable.
  • Cannot loop through the variables.

C programming language offer C arrays as a solution to store the values under a single name. It means, for 1000 variables you need a single name reference.

Definition of an Array

An array is a collective name for a group of similar data types.

The definition tells you all the characteristics of an array. Let’s make a list of these characteristics.

  • Collective name or a single name to refer all values in an array.
  • Similar data type.
  • It is a group of variables ordered by an index.

The only issue with an array is that the memory space reserved for an array is wasted, if not used.

C Arrays Index

Each item in an array has an index value which starts at 0. It is a common mistake to assume that the array starts at 1. The index helps to access the value of any element or elements of an array.

Note:- The index cannot be more than the size of an array.

One-Dimensional Array

A one-dimensional array is a linear arrangement of variables of similar type. You can create an array of int, float, or char.
Declaration of C arrays

To declare an array using the following syntax.

< data_type > < array_name >[< number of elements >];

Initialization of C arrays

There are three way to initialize an array.

int array[3] = { 3, 3, 5 , 3};
int array[]   = {3, 5, 4, 3};
int array[3] = { 5, 3, 5, 3};

Accessing C array

To access an array element use its index value.

int p;
p = array[4];

/* Value of 5th element of an array is assigned to variable p because index = 0, 1, 2, 3, 4 */

int i = 0;
p = array[i];

Reading value to an array element

To read value into an array using the following method.

scanf("%d\n", &array[5]);

Memory Representation of One-Dimensional Array

Each element of an array occupies an address space given below. This location can be identified using an index value which depends on the size of an array. If the size = 8, then index value is n-1 i.e., 8 – 1 = 7;

One Dimensional Array Memory Representation
One Dimensional Array Memory Representation

Example Program

#include < stdio.h >

int main()
{

    int i;
    int arrone[3] = { 2, 3, 4 };
    int arrtwo[] = { 2, 4 };
    int arrthree[4];

/* reading value for arraythree */

    printf("Enter arraythree elements: \n ");
    for (i = 0; i < 4; i++)
    {

        scanf("%d", &arrthree[i]);

    }

/* print value of arrone */

    for (i = 0; i < 3; i++)
    {

        printf("arrone[%d] = %d\n", i, arrone[i]);

    }
    printf("\n\n");

/* print value of arrtwo */

    for (i = 0; i &lt; 2; i++)
    {

        printf("arrtwo[%d] = %d\n", i, arrtwo[i]);

    }

    printf("\n\n");

/* print value of arrthree */

    for (i = 0; i &lt; 4; i++)
    {

        printf("arrthree[%d] = %d\n", i, arrthree[i]);

    }

    printf("\n\n");

system("PAUSE");
return 0;

}

Output-One Dimensional Array

Output - One Dimensional Array - C Array
Output – One Dimensional Array – C Array

Two-Dimensional Array

A two-dimensional array has a row and a column index value for its variables that are of the same type. You can create an array of int, float or char of type two-dimensional.
Declaration of a 2D array

int array[4][3];

The first index is row index value and the second index is column index value. So there are 4 rows and 3 columns in the 2D array we declared.

Initialization of an array

There are three ways to initialize a 2D array.

int array[][]   = { { 3, 3, 5}, {3, 2, 4}, {3, 4, 3} };
int array[][2] = { {1, 3, 3}, {0, 2, 7}, { 3, 6, 2} };
int array[2][2] = { {1, 3, 3}, {0, 2, 7}, { 3, 6, 2} };

Accessing a 2D array

To access a 2D array element use the row index value and the column index value.

p = array[4][3];
int i =0;
int j = 3;
p = array[i][j];

Read into a 2D array

To read value into an array using the following method.

scanf("%d",&array[5][4]);

Memory Representation of a 2D Array

The following diagram is a memory representation of a 2D array. It is a matrix representation with rows and columns representing the 2 dimensions of the array.

2 Dimensional Array Memory Representation
2 Dimensional Array Memory Representation

Example Program – C Arrays

#include <stdio.h>
int main()
{

    int i,j;
    int arr_one[2][3] = {{2,3,4},{2,5,6}};
    int arr_two[][2] = {{2,4},{3,5},{7,8}};
    int arr_three[2][2];

/* reading value for arr_three */

    printf("Enter arraythree elements:\n");

    for(i=0;i < 2;i++)
    {
        for(j=0;j<2;j++)
        {

            scanf("%d",&arr_three[i][j]);
        }
    }

/* print value of arr_one */

    for(i=0;i<2;i++)
    {
       for( j=0;j<3;j++)
       {
       
        printf("arrone[%d][%d] = %d\n",i,j,arr_one[i][j]);
       }
    }

    printf("\n");

/* print value of arr_two*/

    for(i=0;i < 3;i++)
    {

        for( j=0;j < 2;j++)
       {

            printf("arr_two[%d][%d] = %d\n",i,j,arr_two[i][j]);

       }
    }

    printf("\n");

/* print value of arr_three */

    for(i=0;i < 2;i++)
    {
        for( j=0;j < 2;j++)
        {

            printf("arr_three[%d][%d] = %d\n",i,j,arr_three[i][j]);
   
        }
    }

    printf("\n");
system("PAUSE");
return 0;
}

Output – Two Dimensional Array

Output - Two Dimensional Array
Output – Two Dimensional Array

Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

Advertisements