A Brief description of Array in C programming

Teresa
3 min readSep 3, 2022

--

Introduction:

Normally one variable of one data type can hold only one value.

Normally one variable of one datatype can hold only one value

In a single variable, we have stored a single type of data.

Here, the data are stored at different memory locations.

But in case of Array, We don’t need to declare values for elements. Like below:

Many variables holding they same datatype at different locations
Many variables holding the same datatype at different locations

Here, the variables a,b,c,d and e are holding the same type of data in different variables and different locations.

Why need an array?

In short, it stores several data of the same datatype in a consecutive memory location for quicker access and easy implementation under the same variable name.

Definition: An array is a fixed-size sequenced collection of elements of the SAME datatype. The idea is to store multiple items of the same type together.

Syntax to declare array: datatype variable-name [size];

array implementation, Array index starting with 1

We have implemented an array. Here array elements are accessed by their index numbers. the way the declaration is done is int a[5]={1,2,3,4,5}

a[0]= 1

a[1]= 2

a[2]= 3

a[3]= 4

a[4]= 5

One-dimensional:

Just by declaring one variable, we can put an INDEX number which is merely the number of variables.

We can index starting with 0 which is preferred by the computer.

But we shouldn’t get confused so we can state indexing with 1.

Index starting with 0

Compile Time Initialization:

We can initialize the elements of an array in the same way as the ordinary variable when they are declared.

Datatype array-name [size] = {list of values}

int number[3] = {0, 1, 2};

We can omit the size during compile time initialization only.

int number[ ] = {1, 2, 3, 4};

This approach works fine as long as we initialize every element in the array.

Character array Initialization :

char name[ ] = {‘J’,ʻe’,ʻn’,ʻc’,ʻy’,ʻ\0’};

or,

char name[ ]=ʺJency”;

Compile-time initialization may be partial. That is, the number of initializing may be less than the declared size.

int number[5]={10, 20, 30};

Here, the array index number initialized is 5 but there are 3 elements.

The remaining 2 places are Zero and if the array type is char Null.

int number[2] = {1,2,3,4};

In this case, the declared size has more initialized elements. The compiler will create an error.

Run time initialization:

An array can be Explicitly initialized at run time. This approach is usually applied for initializing user input data. Using for loop can help in this case.

--

--

No responses yet