Monday, March 7, 2016

C Arrays

Suppose in your company, if you have 200 employees and you want to store salaries of all 200 employees. For that, declaring 200 different variable is tedious and is impossible to maintain. So, instead of declaring 200 variables you can declare a single array of 200 sizes, where each index refers to different employees.

Declaration of Arrays

Arrays in C can be declared in following way:
<data type> <array name>[<array size>];
For example:
int employees[200];
There are some properties you need to know about arrays in C.
  • Array index starts from 0 i.e. in the above example, array indexes would be from 0 to 199 in general 0 to n-1.
  • Array allocates memory in continuous way.
  • Arrays always have fixed size i.e. you cannot change its size after its declaration.
  • Arrays can be single dimensional as well as multi-dimensional also.

No comments: