Arrays in c

Arrays in C

Array

An array is a collection of similar data types. Arrays are used to store group of data of same data type. Array use to store continuous memory locations.

Example : int a[10];

Types of arrays

  1. One Dimensional Array
  2. Two Dimensional Array
  3. Multi Dimensional Array

1. One Dimensional Array

An array where data is arranged in a single dimension is called one dimensional array.

Declaration of 1-D Array

Syntax : datatype array_name[size];
Example : int marks[3];

Initialization of 1-D Array

Syntax : datatype array_name[size] = { List of value };
Example : int marks[3]={0,1,2};

2. Two Dimensional Array

An array where data is arranged in a two dimension is called two dimensional array. Such as combination of rows and columns.

Declaration of 2-D Array

Syntax : datatype array_name [row-size] [column-size];
Example : int table[2][2];

Initialization of 2-D Array

Syntax : datatype array_name [row-size] [column-size]= { Rows & Column value };
Example : int table[2][2]={0,1,2,3};

3. Multi Dimensional Array

An array where data are arranged in multiple dimensions is called multi-dimensional array. In the sample words as array of arrays known as multi-dimensional array.

Declaration of Multi-D Array

Syntax : datatype array_name [S1] [S2]………..[Sn];
Example : int table[2][3][4];

Passing arrays to functions

Passing array elements to a function is similar to passing variables to a function. In C language we can pass the entire Arrays to Function as an argument.

Example 1 :  int sum (int arr[]);
   // using call by value

Example 2 :  int sum (int* ptr);
  // using call by reference

 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free