Array in C - 👑 सार्थक मुंड S3 🤴🏻

Array in C

 
Array in C

What is Array in C programming. Explain the merits and demerits of array in C.

An array is an identifier to store a set of data with common name. Note that a variable can store only a single data. Arrays may be one dimensional or multi dimensional.

Defining an array one dimensional arrays :-

Definition: Arrays are defined like the variables with an exception that each array name must be accompanied by the size (i.e. the max number of data it can store).For a one dimensional array the size is specified in a square bracket immediately after the name of the array.

The syntax is,

data-type array name[size];


So far, we've been declaring simple variables: the declaration
int i; declares a single variable, named i, of type int. It is also possible to declare an array of several elements. The declaration int a[10]; declares an array, named a, consisting of ten elements, each of type int. Simply speaking, an array is a variable that can hold more than one value. You specify which of the several values you're referring to at any given time by using a numeric subscript. (Arrays in programming are similar to vectors or matrices in mathematics.) We can represent the array a

above with a picture like this:
 

Array in C


 

For Ex.: int x[100]; float mark[50]; char name[30];

Note: With the declaration int x[100],computer creates 100 memory cells with name
x*0+,x*1+,x*2+,.........,x*99+.Here the same identifier x is used but various data are distinguished by the subscripts inside the square bracket.
 

Array Initialization Although it is not possible to assign to all elements of an array at once using an assignment expression, it is possible to initialize some or all elements of an array when the array is defined. The syntax looks like this:

int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};


The list of values, enclosed in braces {}, separated by commas, provides the initial values for
successive elements of the array.



Example :-

#include<stdio.h>
#include<conio.h>

void main()
{

int arr[5]={12,23,34,45,56};
int i;
for(i=0;i<5;i++)
{

printf(“Index %d:%d\n”,i,arr*i+);
}

getch();
}


Output :
Index 0:12
Index 1:23
Index 2:34
Index 3:45
Index 4:56

Advantages :-

  •  It is better and convenient way of storing the data of same datatype with same size.
  •  It allows us to store known number of elements in it.
  •  It allocates memory in contiguous memory locations for its elements. It does not allocate any extra space/ memory for its elements. Hence there is no memory overflow or shortage of memory in arrays.

     
  •  Iterating the arrays using their index is faster compared to any other methods like linked list etc.

  •  It allows to store the elements in any dimensional array - supports multidimensional array.

Disadvantages :-

  •  It allows us to enter only fixed number of elements into it. We cannot alter the size of the array once array is declared. Hence if we need to insert more number of records than declared then it is not possible. We should know array size at the compile time
    itself.
  •  Inserting and deleting the records from the array would be costly since we add / delete the  elements from the array, we need to manage memory space too.
  •  It does not verify the indexes while compiling the array. In case there is any indexes pointed which is more than the dimension specified, then we will get run time errors rather than identifying them at compile time.


Also Read :-

  1. Programs of C

  2. Programs  of C++

  3. Programs of Java

  4. Programs of Python

  5. Codes with HTML, CSS, JS

  6. More Questions & Answers

 

Previous article
Next article

1 Comments

Articles Ads

Articles Ads 1

Articles Ads 2

Advertisement Ads