0% found this document useful (0 votes)
11 views

What is an Array in C

Uploaded by

harshisai23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

What is an Array in C

Uploaded by

harshisai23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

What Is An Array In C?

An array is a collection of similar types of data items at a contiguous


memory location. However, the size of the array is fixed, and you need to
allot its size at the time of declaration.

1 4 8 25 2 17

The elements in this array are 1, 4, 8, 25, 2, and 17, and they are stored at
indexes starting from 0 to 5 (size-1). However, it is essential to declare the
data types of the elements stored in the array while declaring. The basic
syntax for declaring an array in C is given here.

Data_type array_name[ array_size ];

Let us name our array “ arr” and declare it using the above syntax. The
data type of our array elements will be integer, and the size is 6.

1 4 8 25 2 17

arr [0] arr[1] arr[2] arr[3]


arr[4] arr[5]

Page |1
In C language, we can declare an array by first naming the data_type of the
items stored in the array. Keep in mind that the data types of all the elements
inside the array need to be similar. Now, name your array using

the naming guidelines for C with the size of the array. When we declare an
array in C, our compiler will continuously allocate that memory block to the
array.

Why do we need Arrays?

Array stores an extensive collection of similar data types. We have only three
variables, and then we can easily store them using separate names like int
var1, int var2, and int var3, but what if we have an extensive collection of
these variables? Then, we cannot assign separate names to each of them, as
it would be tedious and time-consuming.

Here comes the use of arrays in C, where we can store multiple data type
values in a contiguous memory block. Hence, we can use an array in C when
we are working with a large number of similar items.

Accessing Elements in Arrays

You can easily access elements in an Array using their index. Suppose we
want to access the element at the third index of our array. Then, we can
easily do it using arr[2], which is size-1.

Access Elements in an array

int arr[6] = {1, 4, 8, 25, 2, 17};

printf (“ %d” , arr[2]); // Accessing the third element at index 2, which is 8

Also, you can quickly iterate through arrays with loops and conditional
statements. It allows quick and easy retrieval of data items.

Array in C Initialization and Declaration

Page |2
You need to initialise your array with some initial value at the time of
declaration. This is done because when declaring arrays in C, they contain
garbage values inside, which can alter our desired output. However, the
easiest way to initialise the array is using the method below.

Array initialization using simplest approach

arr [0] = 1; // initialising array in C

arr [1] = 4;

arr [2] = 8;

arr [3] = 25;

arr [4] = 2;

arr [5] = 17;

Hence, we need to initialise our array with some starting value. There are a
few methods for declaring and initialising our array.

1. Initializing Array In C During Declaration

Let us initialise our array with a proper size and elements inside. This list
contains the elements enclosed within curly braces {}.

Data_type name_of_array [ size ] = { value1, value2,… … ,valueN};

Array in C

int arr[6] = { 1, 4, 8, 25, 2, 17}

2. Initializing Array In C Without Size

Let us initialise our array without a proper predefined size and elements
inside. This list contains the elements enclosed within curly braces {}. In this
method, the compiler automatically detects the size of the array.

Page |3
Data_type name_of_array [ ] = { value1, value2,… … ,valueN};

Array without size

int arr[ ] = { 1, 4, 8, 25, 2, 17}

3. Declaring Array in C with Loops

In this declaration method, we will use the loops to declare our array. We will
use the loop to assign values to each element and insert them in our array.

Array using loops

for( int i=0 ; i<N; i++){


name_of_array[ i ] = value i;}

Let us understand this with the help of the example we took above. Suppose
we have six elements: 1, 4, 8, 25, 2, 17. We want to insert them in a loop.

Array in C

#include <iostream>

int main() {

int elements[] = {1, 4, 8, 25, 2, 17};

int arrSize = sizeof (elements) / sizeof (elements[0]);

// Initializing array elements using for loop

for (int i = 0; i < arrSize; i++) {

elements[i] = arr[i];

Page |4
cout << “ Array elements after initialization using for loop:” <<endl;

for (int i = 0; i < arraySize; ++i) {

cout << elements[i] << ” “ ;

cout <<endl;

return 0;

Example of Array in C

Let us take an example to understand the working of Array in C using a


simple programming problem. We will use the naive sorting method to sort
the elements inside of our array.

Sorting in Array

#include<stdio.h>

void main ()

int i, j,temp;

int a[6] = {1, 4, 8, 25, 2, 17 };

for(i = 0; i<6; i++)

for(j = i+1; j<6; j++)

Page |5
if(a[j] > a[i])

temp = a[i];

a[i] = a[j];

a[j] = temp;

printf (“ Printing Sorted Element List … \n” );

for(i = 0; i<6; i++)

printf (“ %d\n” ,arr[i]);

Update Element inside an Array

Suppose we want to update any element at a given index inside our array,
then we simply need to use an assignment operator with the given syntax.

Name_of_array [ i ] = updated_value;

Update Elements in an Array

arr [ 0 ] = 9;

This will update the element at the index 0 with a new value i,e. 9.

Page |6
Types of C Array

There are mainly two types of C array given here.

1. One-Dimensional Array

This is also known as a 1-D array as it is only having one dimension.

We can easily declare a one-dimensional array using the same syntax we


learned above.

name_of_Array [ size ];

2. Multi-Dimensional Array

This is also known as a multidimensional array because they have more than
one dimension. We can have N- D arrays in C. Let us check the syntax for the
N-D array.

name_of_Array [ size ] [ size2 ];

Recommended Technical Course

● Full Stack Development Course


● Generative AI Course
● DSA C++ Course
● Data Analytics Course
● Python DSA Course
● DSA Java Course
Advantages of using Array in C

Arrays are used to store a large set of similar data items in a contiguous
memory blocks. Let us check the advantages of the array in c.

● With the help of an array we can store a large amount of data in a


contiguous memory location.
● Random access to any elements is possible inside our array using
their index.
● We can easily traverse our array using any loops available.

Page |7
● As the elements are stored in a contiguous memory location, there are
no overhead costs involved. Hence, they are memory efficient.
Disadvantages of using Array in C

There are also a few shortcomings of using an array in C. Some of them we


will check below.

● Array in c has a fixed size, therefore we need to specify the size of the
array at the time of declaration.
● Using an array leads to memory wastage when you declare a size
more than the actual required.
● Insertion and deletion in an array are difficult. There are many other
efficient data elements for this purpose.
● Also, for searching any elements in an unsorted array we need to
traverse the whole array.
vv

Page |8

You might also like