C Programming language

Arrays in C: Introduction
Previous Home Next

Arrays

Array by definition is a variable that hold multiple elements which has the same data type. Array is variables that hold multiple elements which has same data type. An array is a multi element box, uses an indexing system.

Declaring the array

We can declare an array by specify its data type, name and the number of elements the array holds between square brackets immediately following the array name.
Name;
Type of array
Number of element

There are some rules for array declaration

The data type can be any valid C data types including structure and union. The array name has to follow the rule of variable and the size of array has to be a positive constant integer. We can access array elements via indexes array_name[index]. Indexes of array starts from 0 not 1 so the highest elements of an array is array_name[size-1].

Initializing the array

It is like a variable, an array can be initialized. To initialize an array, you provide initializing values which are enclosed within curly braces in the declaration and placed following an equals sign after the array name.

int list[5]={2,3,4,5,6}  OR   int list[5] = {2,1,3,7,8};

Character arrays

char string1[]="first";
Here ,we using char string and its array size is six.

char string[]={'f','g','t','y','u','I','\0'};
//\0 null character terminating the string.

Passing the array

To pass an array argument in a function specifies the name of array to pass without any bracket.

int myarray[24];
myfunction(my array,24).
array usually pass to the function
array using call by reference
function knows where the array stored

passing array element

  1. using call by value
  2. pass subscripted name (ex: myarray[5]) to function.

Function prototype

void modify array (int b[],int array size);
Parameter name may be optional.
int b[] may be int[]
int array size may be int
Previous Home Next