Skip to content

Arrays in C

Arrays in C : An array is described as the gathering of comparable varieties of information gadgets saved at contiguous reminiscence locations. Arrays are the derived information kind in C programming language, which could store the primitive type of information along with int, char, double, float, etc. It also has the functionality to shop the gathering of derived information types, pointers, shapes, etc. The array is the handiest information shape wherein every information detail may be randomly accessed by its index number.

Arrays in C are helpful when you have to shop for comparable elements. For example, if we need to shop the marks of a scholar in 6 subjects, then we do not want to outline distinct variables for the marks withinside the specific concern. Instead, we can draft an array that may shop the marks on the contiguous reminiscence locations in every situation.

Why do we want Arrays in C Programming?

Consider a situation wherein you want to discover the common of 10 integer numbers entered through the user. In C, you’ve got approaches to do this: 1) Define ten variables with int information kind after which carry out ten scanf() operations to save the entered values withinside the variables after which at ultimate calculate the common of them. 2) Have an unmarried integer array to save all of the values, loop the array to save all entered values, and later calculate the common. Which answer is higher in line with you? The second one answers that. It’s handy to save equal information sorts in a single unmarried variable and later get the right of entry to them using the array index (we can talk about that later in this tutorial).

Types of Array in C

The types of arrays are :

  • One dimensional array
  • Multi-dimensional array

In the tutorial below, we will learn more about the detailed types of arrays.

Declaring and using an array in C


To claim an array, you certainly want to specify the array elements, the variable call and the array size. For example, in case you need to claim an integer array with four elements, you’d use

data_type array_name[array_size];
int a[8];

This declaration allocates a contiguous block of reminiscence for 4 integers and initialises all values to 0. This is how it’s far specified in reminiscence :

Int int int int int int int int

0 0 0 0 0 0 0 0
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

 

Array indexes begin from 0 and cease with (array size – 1). So for the above array, you could use the primary detail with a[0], 2d detail with a[1], 1/3 detail with a[2] and fourth (last) detail with a[7].

Why does Array Indexing start with 0?

Let’s understand this, for example. Suppose you can declare an array of size 10 in the following way:

int x[10];

 

Here x itself is a pointer containing the memory location of the first element of the array. They are described as x[0] * (x + 0), decoded internally by the compiler to access the first element.

Similarly, the second element can access x[1] or *(x+1). Since x contains the address of the first element, you need to add 1 to it to access the second element. So here, it is depicted as *(x + 1). In an array, the index represents the offset of the first element, i.e. the distance from the first element.

Here, suppose the index of the array starts at 1, not 0. In this case, to access the first element, you have to write x[1] decoded to *(x+1 – 1) inside. Note that needs to run one additional operation here, namely subtract by 1. This extra work significantly slows performance if the program is enormous. So to avoid this different manipulation and improve performance, indexing of arrays starts at 0, not 1.

Initialisation of an Array in C

Arrays can be initialised when declared, just like any other variable.

  • Place the initialisation data in curly braces {} after the equal sign. Note the use of commas in the following examples.
  • An array can be partially initialised, providing fewer data items than the size of the array. The rest of the array elements will be automatically initialised to 0.
  • If the array is fully initialised, the size of the array is optional. The compiler will automatically resize the array to fit the initialised data.

For example:

int mark [6] = {18, 3, 6, 11, 10};

You can also initialize an array like this. int mark [] = {18, 3, 6, 11, 10}

We did not specify the size here; however, the compiler knows its length is 5 because we initialise it with 5 elements.

Here,

Change value of the array elements

Input data into the array

Let’s start with a simple program that reads five numbers into an array and outputs them. Below is a picture of the source code of the program.

First, we initialise, proclaim an array of 6 elements and a variable i that we will use in our for loop.

The first loop reads a number from the user and sets it as a[i] element. The elements of an array simply behave like regular variables. An int variable named p uses scanf(“%d”,&p)to set the value of p from user input. Similarly, here we used scanf(“%d”, &a[i]) to set the value of a[i] from user input.

The second loop just uses printf() to print the variables one by one.

Access Array Elements

You can access the elements of the array in the index.

Suppose you declare an array [mark] display as above. The first element of the array is displayed as mark[0], the second element is displayed as

mark[1], and so on.

mark[0] mark[1] mark[2] mark[3] mark [4]

Few keynotes :

  • The first index of the array is 0, not 1. In this example, mark [0] is the first element
  • If the size of the array is n, use n-1 indexes to access the last element. This example assumes that the starting address of mark [4]
  • Mark [0] is 2120d. After that, the address of mark [1] becomes 2124d. Similarly, the address of mark [2] is, for example, 2128d. This is because the size of the float is 4 bytes.

Access elements out of scope!

Suppose you declare an array of 6 elements.

Linear search in an array

In some cases, you need to retrieve an element of an array. For example, given the array {10,20,30,40}, you might want to know whether 30 exists in the array.

A simple way to search for linear search elements is to iterate through all the details in the array and see if it matches the number you want.

Below is a picture of the source code of the program.

The program declared an element to search for an array a loop variable i search. I also declared another variable pos which keeps track of the array index where the element I’m searching for was found at pos to 1. The reason for doing this will become clear later.

The last loop implemented the linear search logic. Iterate through each element of the array and check whether a[i] equals the search value. In the same case, since the index i element was found, it sets the variable pos to the current index i. The next element is found, and we break out of the loop because we don’t need to process it further.

Next, compare the values ​​of pos. The minimum value of an array index can be zero. So if the value of pos is 1, it means that there is no matching item and prints a message like “30 not found”. Otherwise, it prints a message such as “30 found in position 2”.

Multidimensional arrays

In C programming, you can create an array of arrays. Such an array is known as a multidimensional array. E.g.,

Here, x is a two-dimensional (2d) array. The array can hold 16 elements.

Similarly, you can declare a three-dimensional (3d) array, for example.

Here, the array y can hold 30 elements.

There are two types of arrays in Multidimensional Array :-

  1. Two Dimensional Array
  2. Three Dimensional Array

Two Dimensional Arrays

Reading user-entered numbers into a 2D array.

So far, we’ve checked out arrays in which the detail stores are accessible records, kind of like int. These arrays are once in a while referred to as one-dimensional (1D) arrays.

Just as int or float are records types, an array is likewise the kind of a record. Therefore, you may construct an array whose character factors are 1D arrays. These types of arrays are referred to as two-dimensional (2D) arrays.

To declare a two-dimensional array in C, you need:

  1. Primitive Data Type
  2. Variable Name
  3. Size of 1D Array
  4. The number of 1D arrays combined makes up a 2D array

An array of arrays is called a 2D array. The two-dimensional (2D) array in C programming is also known as a matrix. A matrix can be represented as a table with rows and columns.

Addition of Matrices

Adding both matrices requires the same number of rows and columns. Adding two matrices can take numbers from the same position and add them to put the result in the same situation.

Here below is a picture of the source code program adding two matrices.

Output :

Three Dimensional Arrays

In the previous sections we saw 1D and 2D matrices. Any matrix with more than one dimension is a multidimensional matrix, and the concept can be extended to any number of dimensions, the basic concepts being the same.

For example, a three-dimensional (3D) array is a combination of several 2D arrays, and you can initialize a three-dimensional array by:

int a[4][3][3];

This statement creates a 3D matrix that is a combination of three 2D 2 × 2 matrices. As with all matrices in C, the memory allocation is contiguous and all elements are initialized to zero.

To use an element of the array, you must use three indexes. For example, if you type a [0] [1] [1], the second row and the second column of the first 2D matrix are accessed.

nv-author-image

Vaibhav Kapoor

A professional web entrepreneur, WordPress developer and digital marketing strategist with more than 4 years of experience in building a business from scratch. Knowledgeable about SEO, blogging, internet marketing, social media and website development.

Leave a Reply

Your email address will not be published. Required fields are marked *

[wpfepp_submission_form form="1"]