Array in C

For Complete YouTube Video: Click Here

In this class, we will understand Array in C.

Before understanding arrays in C, we will understand more on variables.

The variables in C are of two types.

  1. Scalar Variables
  2. Aggregate Variables
Arrays in C Variables in C
Variables in C

Scalar variables are capable of holding a single data item.

The variables that we have seen so far, like integer, float, double, and character, are scalar.

Aggregate variables can store a collection of values.

In aggregate variables, we have two types.

  1. Arrays
  2. Structures 

Arrays

An Array is a data structure containing several data values, all of which have the same data type. 

Each value is called an element of an array.

How to declare an Array

The image below shows how to declare an array in c.

Array in C Declaration
Array in C Declaration

The int in the declaration states that all the elements in the array of integer type.

When we declare an array, we have to use open and closed square braces after the array’s name to provide the number of elements in the ‘array.’

In the above declaration, we have created an array name ‘a’ with ten elements in it. All the elements are of integer type.

Visualizing an Array

The image below shows how we can visualize an array.

Array in C Visualization
Array in C Visualization

In the above image, ‘a’ is an array of ten elements.

Each box in the above image is a space to store an element of an array.

As we have ten boxes, we can store ten elements.

Accessing the elements of an array

In the above declaration, we haven’t assigned any values to an array.

Assume that we have assigned ten values into the array.

We will try to understand how to initialize and assign values into an array in our later classes.

To access the elements of an array, we have to use the index provided above each element of an array.

Always the index of the array starts with zero.

With n elements in the array, we have a maximum index of an array as n-1.

The image below is the visualization of an array with indexing.

Array in C with values
Array in C with values

With ten elements in the array, we have the first element index as zero and the last element index as nine. The process of indexing is called array indexing.