Array in C

Array in C

 What is array in C programming?

Array in C is a variable that is used when multiple data have to store in a single variable. An array is a data structure that stores multiple data items as a single entity. Data types of entire data must be the same. The individual data is called an element of an array. 

It may be convenient to store a collection of similar data elements in different separate variables. For example, if the age of 100 persons were to be store in the variable in unique names, it certainly would be difficult, which means 100 variables need for assigning the individual values. 

So an array certainly solves this problem because basically, the variable name is the same. We differentiate among the values in an array by its unique subscripts with defined size.

Syntax of array 

         data type_Variable[size]; 

Example of array

          int a[5];

          float b[15];

          char [15][20];


In this above example

      int a[5];

Where int is a data type, a is a variable, and the meaning of [5] is the variable a contains 5 integers number.


Let the above picture is a memory. 0, 1, 2, 3, 4 are addresses of an array and multiple data given by users are store in different addresses. That all are stored in a single variable. The data type of all data must be the same.

similarly,

     float b[15];

Where float is a data type, b is a variable, and the meaning of [15] is the variable b contains 15 float numbers.

Must be remember

An array is a collection of similar types of data items treated as single units. It acts to store related data under the same name with an index, also known as a subscript which helps to access individual array elements. The array data type may be int, float, char, etc., depending upon the nature of the problem.

Characteristics of Array

  • All the array elements share the common name.
  • The element of the array is stored in contiguous memory locations.
  • By declaring or using an array, the program becomes short and simple which handles a large volume of similar kinds of data items.
  • We put the array size of fixed length as required which means once the size is declared then the size will be fixed at the execution time of the program so-called static types.
  • We can randomly access every element using the numeric index, index starts from 0 and ends at size -1

Advantages of Array

  • It is easier for handling similar types of data in a program.
  • It is efficient for solving problems like sorting, searching, indexing, etc.
  • It is very close to the matrix, therefore it is easy to solve matrix-related problems.
  • Graphics is an array of pixels so, graphics manipulation can be easily be done using an array.
  • Disadvantages of Array

Disadvantage of Array

  • It is not possible to hold dissimilar type of data in an array.
  • It is difficult to visualize the multi–dimensional array.
  • It is static in nature so it is difficult to define the size of the array during running time.

Types of array 

There are two types of array, which are as follows.

Single dimension array 

An array that has only one subscript is named one dimensional array, a subscript is a number of the large brackets in which we put the size of the array.

syntax: data_type array_name[array_size];

example int a[10];

              float b[20];

Multi dimension array

An array that has more than one subscript is named multi dimensional array, subscripts define each dimension of the array.

Syntax data_type array_name[expression 1][expression 2];

example int a[20][25];

             float b[15][5];

Note:- Generally Multi dimension array use in matrix.

Also, check 

 Program of a single dimension array

Click here

Program of multi-dimension array

Click here


Post a Comment

0 Comments