Array in C

Array in C Language with Examples

In this article, I will discuss the Array in C Language with Examples. Please read our previous articles discussing the Preprocessing Directives in C.

What are Arrays in C?

The array is defined as a collection of similar data elements. If you have some sets of integers and some sets of floats, you can group them under one name as an array.

Method of Declaring an Array

If you want an integer-type array, let’s say int A[5];

Then, you will get 5 integers, and the array name is A, and you can access all those integers using the name A. So, A is an array, and you get 5 integers spaces, and the indices will be 0,1,2,3,4. So, 5 locations mean indices will start from 0 and end at 4, a total of 5 spaces. For a better understanding, please have a look at the below image.

Method of declaring an array

Now, we can store 5 integers. Every location is an integer. If we assume an integer is taking 2 bytes, then there are a total of 10 bytes, i.e., 2 bytes each. Now, each location can be accessed like A[0]=27; //Here, 27 will be stored in the array A with indices 0. Now, if I store A[1]=10; then 10 is stored in a place with indices 1. For a better understanding, please have a look at the below image,


So, we can have a group of elements in a single place.

How to Declare and Initialize an Array in C Language?

I’ll show you how to declare and initialize an array in c language with an example. Please observe the following code. Suppose I want to declare an array A of size 5 in the main function. Then, you need to declare the array as shown below.

int main()
    int A[5];

Now, an array of size 5 will be created. When a program runs, it is inside the main memory. The main memory is divided into 3 sections, i.e., the Code section, the Stack section & Heap section, as shown in the below image.

How to declare and initialize an array?

As you can see in the above image, whatever code we write will be inside the code section. You need to understand that the variables we declared will be created inside the Stack section. So, here, the Array will also be created inside the Stack as the array is also a variable.

Those variables that are created inside the stack section are directly accessed by the Main method from the code section. So, the array is directly accessible to the main function and can directly store the values in the array. I hope you understand how the array is declared and where the array is created inside the main memory.

How to Declare and Initialize an Array in C?

You can declare and initialize an array in the same line as shown in the below example.

int main()
     int A[5]={1,3,5,7,9};

The above code shows the declaration of an array with size 5. The array is also initialized with the values 1, 3, 5, 7, 9. So, this is a declaration as well as the initialization of an array. The memory representation of the above is shown below.

How to declare and initialize an array?

How to Access an Array in C?

We can access all the elements of an array one by one using a for loop. To understand this better, please have a look at the following code.

int main() 
     int A[5]={2, 4,6,8,10};
     int i;

I hope you understand the basics of the array. Let us proceed and understand the array in depth.

What is an Array in C?

An array is a derived data type in C that is constructed from the fundamental data type of the C programming language. An array is a collection of similar types of data elements in a single entity. We recommend creating an array when we require n number of values of the same data type.

When working with arrays, memory is always constructed continuously, which is why it is possible to access the data randomly. When working with arrays, all values will share the same name with a unique identification value called index.

Array index starts with 0 and ends with (size-1). When working with arrays, we must use an array subscript operator, i.e., [ ], to set or get the values from an array. Always array subscript operators require one argument of type unsigned integer constant, whose value is always > 0 only.

What is an Array in C

Why do we need Array?

We can use normal variables (v1, v2, v3, etc.) when we have a small number of objects, but if we want to store many instances, it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable. For example, if we want to store a student’s scores in 6 subjects, then we don’t need to define different variables for the scores in the different subjects. Instead, we can define an array that can store the scores in each subject at the contiguous memory locations. By using the array, we can easily access the elements.

Properties of Array in C:

The array contains the following properties:

  1. Each element of an array is of the same data type and carries the same size, i.e., int = 4 bytes.
  2. An array can store multiple values of a similar type, which can be referred to by a single name.
  3. The array name is actually a pointer to the first location of the memory block allocated to the array’s name.
  4. Any particular element of an array can be modified separately without distributing other elements.
  5. All elements of an array have the same name, and they are distinguished from each other with the help of index position.
  6. Elements of the array are stored at contiguous memory locations.
  7. Elements of the array can be randomly accessed using the index position.
Advantages of Arrays in C Language
  1. Code Optimization: Only a few lines of code are required to access the data by using an array.
  2. Ease of traversing: By using the for loop, we can easily traverse the elements of an array.
  3. Ease of sorting: Only a few lines of code are required to sort the elements of an array.
  4. Random Access: We can randomly access any elements using an array.
Disadvantage of Arrays in C Language
  • Fixed Size: Whatever size we define at the time of declaration of the array, we can’t exceed the limit. So, it doesn’t grow in size dynamically.
  • Insertion and deletion Costly: Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.
Operation of an Array:
  1. Traversing: Process each and every element in the array sequentially.
  2. Searching: Searching an element to find out whether the element is present or not.
  3. Sorting: Arranging the elements in an array in a particular sequence.
  4. Inserting: To insert the element into the array.
  5. Deleting: To delete the element from the array.
Types of C Arrays:

There are 2 types of C arrays. They are,

  1. One dimensional array
  2. Multi-dimensional array (Two-dimensional array, Three-dimensional array, Four-dimensional array, etc…)

In the next article, I will discuss the One-Dimensional Array in C with examples. In this article, I try to explain Arrays in C Language with Examples. I hope you enjoy this Arrays in C Language with Examples article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

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