C Array

Array is a collection of similar data type items. Arrays are used to store a group of data of the same datatype. Arrays can of any datatype. Arrays must have constant size. Continuous memory locations are used to store array. Array index always starts with 0.

Syntax

Types of Array:

  • One Dimensional Array
  • Two Dimensional Array
  • Multi-Dimensional Array

One Dimensional Array

An array which has only one subscript is known as a one dimensional array.

Syntax:

Array declaration

Array initialization

Accessing array

An element of an array can be accessed by its index number. And that is done by placing the index number of the required elements within the square brackets ‘[]’ after the array name.
For example −

Two Dimensional Array in C

An array, which has two subscripts is known as a two dimensional array. By using two-dimensional arrays, programmers can manipulate data structures. It has two subscripts, the first one represents the row and the second one represents a column. Subscripts are always starts at 0.

Syntax:

Array declaration

Array initialization

Accessing array

An element of a two-dimensional array can be accessed by using its subscripts, that is row index and column index of that array.

For example −

Multidimensional-arrays in C 

An array is typically a data structure meant to store a collection of elements to which is associated with a given index. These elements are often in contiguous memory locations, although this is not a requirement.
Multidimensional-arrays can be described as multi-dimensional tables. Each index used in order to find a given element is called a dimension.
Depending on the programming language, such a data structure can be achieved through a specific multidimensional array data structure or in the shape of an “array of arrays”.

Syntax

Array Declaration

Array initialization

Passing Array To Function in C

Passing array to the function name of the array is passed as actual arguments and array variables with subscript are passed as formal arguments.
There are three different ways to do pass array to a function are:

First:

Second:

Third:

Pointer to an array

Pointer to an array is basically a pointer that points to the address of the first element of the array.

Example

In the above code, the variable total will provide the base address, which is a constant pointer pointing to the first element of the array that is to total[0]. And hence the total will contain the address of total[0]. Thus, the above program fragment assigns p as the address of the first element of the total.