C PROGRAMMING: ARRAYS

C PROGRAMMING: ARRAYS
  • An array is a collection of data items of the same type.
  • Items are started at contiguous(different) memory locations.
  • It can also store the collection of derived data types, such as pointers, structures, etc.
  • A one-dimensional array is like a list.
  • A two-dimensional array is like a table.
  • The C language places no limits on the number of dimensions in an array.
  • some text refers to one-dimensional arrays as “vectors”, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also can store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

In C languagearrays are referred to as structured data types. An array is defined as a finite ordered collection of homogenous data, stored in contiguous memory locations.

Here the words,

  • finite means data range must be defined.
  • ordered means data must be stored in contiguous memory addresses.
  • homogenous means data must be of a similar data type.

Declaring an Array:

Like any other variable, arrays must be declared before they are used. General form of array declaration is,

data-type variable-name[size];

/* Example of array declaration */

int arr[size];

Initialization of an Array

After an array is declared it must be initialized. Otherwise, it will contain garbage value(any random value). An array can be initialized at either compile-time or at runtime.

We can initialize the c array at the time of declaration. Let’s see the code.

int marks[5]={20,30,40,50,60};

In such case, there is no requirement to define the size. So it may also be written as the following code.

int marks[]={20,30,40,50,60};  

Properties of Array:

  • Each element of an array is of the same data type and carries the same size, i.e., int = 4 bytes.
  • Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.
  • Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.

why do we need array ?

  • Code that uses arrays in sometimes more organized and readable.
  • If you were to store the marks in a test of 56 variables will make the program look cluttered and messy.
  • the solution to this is an array!

Advantage of Array:

1) Code Optimization: Less code to the access the data.

2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantage of Array:

1) 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 the size dynamically like LinkedList which we will learn later.

Example where arrays are used:

  • to store list of Employee or Student names,
  • to store marks of students,
  • or to store list of numbers or characters etc.

Since arrays provide an easy way to represent data, it is classified amongst the data structures in C. Other data structures in C are the structure, lists, queues, trees, etc. An array can be used to represent not only a simple list of data but also a table of data in two or three dimensions.

syntax for declaring and initializing an array:

  • Data_type name [size];
  • Data_type name [size] = {x,y,z,…}; //size not required in this case!(for 1-D array)
  • Data_type name [rows][columns]; //for 2-D arrays
  • We can also initialize the array one by one by accessing it using its index:

for example: name[0] = 0;

Accessing elements of an array:

The elements of an array can be accessed by specifying an array name followed by subscript or index inside square brackets (i.e []). Array subscript or index starts at 0. If the size of an array is 10 then the first element is at index 0, while the last element is at index 9. The first valid subscript (i.e 0) is known as the lower bound, while the last valid subscript is known as the upper bound.

int my_arr[5];

then elements of this array are;

First element – my_arr[0]
Second element – my_arr[1]
Third element – my_arr[2]
Fourth element – my_arr[3]
Fifth element – my_arr[4]

Array subscript or index can be any expression that yields an integer value. For example:

int i = 0, j = 2
;my_arr[i]; // 1st element
my_arr[i+1]; // 2nd element
my_arr[i+j]; // 3rd element

In the array my_arr, the last element is at my_arr[4], What if you try to access elements beyond the last valid index of the array?

printf(“%d”, my_arr[5]); // 6th element
printf(“%d”, my_arr[10]); // 11th element
printf(“%d”, my_arr[-1]); // element just before 0

Sure indexes 510 and -1 are not valid but C compiler will not show any error message instead some garbage value will be printed. The C language doesn’t check bounds of the array. It is the responsibility of the programmer to check array bounds whenever required.

examples of 1-d array:

#include<stdio.h>
 
int main()
{
    int arr[5], i;
 
    for(i = 0; i < 5; i++)
    {
        printf("Enter a[%d]: ", i);
        scanf("%d", &arr[i]);
    } 
 
    printf("nPrinting elements of the array: nn");
 
    for(i = 0; i < 5; i++)
    {
        printf("%d ", arr[i]);
    }
 
    // signal to operating system program ran fine
    return 0;
}

output:

Enter a[0]: 11
Enter a[1]: 22
Enter a[2]: 34
Enter a[3]: 4
Enter a[4]: 34
 
Printing elements of the array:
 
11 22 34 4 34

EXAMPLES OF 2-D ARRAY:

#include<stdio.h>  
int main(){      
int i=0,j=0;    
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};     
//traversing 2D array    
for(i=0;i<4;i++){    
 for(j=0;j<3;j++){    
   printf("arr[%d] [%d] = %d n",i,j,arr[i][j]);    
 }//end of j    
}//end of i    
return 0;  
}    

output:

arr[0][0] = 1 
arr[0][1] = 2 
arr[0][2] = 3 
arr[1][0] = 2 
arr[1][1] = 3 
arr[1][2] = 4 
arr[2][0] = 3 
arr[2][1] = 4 
arr[2][2] = 5 
arr[3][0] = 4 
arr[3][1] = 5 
arr[3][2] = 6

The two-dimensional array can be defined as an array of arrays. The 2-D array is organized as matrices which can be represented as the collection of rows and columns. 

 I hope everythimg is clear to you, In the next article we will dicuss about "POINTERS".
I will see you next time...
Also visit our previos articles listed below:

And many more…

Scroll to Top