C Arrays

In c, Array is a set of similar values, stored in continuous memory location. Each value of array is called as element. Array elements are accessed by its index. Array index is always an integer value starts with 0 to number of element - 1.

Why array is required

Variables with primitive data types like int, char, float or double are able to store only single value i.e. one value at a time. If a new value is assigned to a variable, previous value is overwritten by new value. In such case, Array allows to store more than one value in a single variable of same data type.

Example

int val[10];

Array Declaration

Declaration informs the c compiler the type of data and the number of values the array will store.

Syntax

<data_type>  array_name[size];

Example

int val[10];
  1. int is data type, so val[10] is an integer array which holds only integer value.
  2. [] it is subscript operator, used to declare array, It always comes after variable name like val[10].
  3. Here 10 inside val[10], means length of an array, which specifies array can hold 10 values of given data type.

Arrray size and Initialization

If array is not initialized at declaration time, then array length or size must be specified like val[10].

int val[10];

If array is initialized, then array length or size is optional.

int val[] = {20, 30, 40, 50, 60};
int val[5] = {100, 200, 300, 400, 500};

Both above examples, will work.

  1. Array size or length must be specified at declaration.
  2. Array size cannot be modified at runtime.

Access array elements

In c, array elements are accessed by its index, which always starts with 0. and index is always is an integer.

Example

#include <stdio.h>

int main()
{
    // creating array of integers
    int val[10] = {1,2,3,4,5};

    for(int i = 0; i < 5; i++){
       printf("%d ",val[i]);
    }
    return 0;
}
Output:
1 2 3 4 5

Types of array in c

On the basis of [] dimensions, c has following types of array

  1. One dimensional array
  2. Two dimensional array
  3. Three dimensional array
  4. Multidimensional array

One dimensional array

In c, One dimensional array is used to store data in an array in linear form (one after another).

Syntax

<data_type> variable_name[size];

Example

#include <stdio.h>

int main()
{
    // creating array of character
    int val[10] = { 1,2,3,4,5,6,7,8,9,10 };

    for(int i = 0; i < 10; i++){
       printf("%d ",val[i]);
    }
    return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10

Two dimensional array

Two dimensional array called as matrix or 2D array, because in two dimensional data is stored as rows and columns format. (Tabular format)

Syntax

<data_type> variable_name[row_size][column_size];

Example

#include <stdio.h>

int main()
{

    // Declaring and initializing a 2D array
    int val[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12};

    for (int row = 0; row < 3; row++) {
        for (int column = 0; column < 4; column++) {
            printf("%d ",val[row][column]);
        }
        printf("\n");
    }

    return 0;
}
Output:
1  2  3  4
5  6  7  8
9 10 11 12

Multidimensional array

An array which has more than one dimensions called multidimensional array. Examples are 2D array, 3D array. More the dimension, more the difficultly to maintain code.