C Arrays

Array: 


  • An array is a collection of two or more adjacent cells of similar type.
  • Each cell in an array is called as array element.
  • Each array should be identified with a meaningful name and the number of adjacent cells to be associated for that array should be specified.

Single/One-Dimensional Arrays

The one dimensional arrays are also known as Single dimension array and is a type of Linear Array. In the one dimension array the data type is followed by the array name which is further followed by the size (an integer value) enclosed in square brackets and this represents the dimension either row wise or column wise.

Declaration of Array: 

To declare an array the following syntax should be followed:

 <data type>  arrayname[SIZE];

· where data type can be any fundamental or basic or instrinsic or implicit  or user defined data type.
· Array name should follow the rules of an identifier.
· SIZE takes an integer value that specifies the number of adjacent cells required for that array.

For e.g., int number[10];
where number is the name of the array, the integer value 10 indicates the size of the array i.e., 10 adjacent cells are allocated with each cell storing integer element as the data type is int.
The memory is allocated with index starting from zero and ends with its size-1. Pictorially the memory allocation and its identification is represented as shown below:

Array name
Number[0]
Number[1]
..
..
..
..
..
..
Number[9]
Element










Initializing elements to the array:


Elements can be initialized to the array as shown below. We initialize values to the array only if the values are going to be fixed.

General syntax:   

<data type>  arrayname[SIZE]={value1,value2,….,valueN};  

For e.g. for the above declarations we will see how to initialize (assigning) values.

int number[10]={10,20,30,40,50,60,70,80,90,12};
    ( or )
number[0]=10;
number[1]=20;
number[2]=30;
.
.
number[9]=12;

where 10 is assigned to location number[0],20 is assigned to location number[1] and so on.


Referencing the Array:

An array can be referenced in the program in any of the executable statements in either one of the ways given below:

Way 1:

Syntax is given below: arrayname[index];
where index takes an integer value, this integer value specifies to the array location which is index + 1, this integer value also should not be greater than the size of that array specified in the declaration of the array.

For e.g.,           int number[15];

to refer 11th location of this array we go for number[10]

Way 2:

Syntax is given below:
Variable=value;
arrayname[variable];

Where an integer value is to be initialized to a variable and that variable is used within the square brackets of that array and this variable is called as subscript variable as this variable contains an integer value that refers to the index of that array.

Way 3: Using for loops for sequential access:


In order to read elements into the array sequentially or to access elements of an array sequentially we make use of looping constructs. We will see how this is done.

For reading elements into the array sequentially:

For e.g. for the declaration given below, we will see how elements are read sequentially into the array.

int number[10];    /* array declaration  */

/* reading elements into the array sequentially */

for ( i=0; i < 10 ; i++ )
scanf( “ %d ”, & number[ i ] );

Explanation:

For loop is used to vary the index of the array from 0 to its size-1.The variable used in for loop is used in the scanf statement along with the array name enclosed in square brackets. This variable is called as subscript variable.


For printing elements of the array sequentially:

For e.g. if we suppose assume that the array is declared and the elements are already initialized, then we will see how elements are accessed sequentially from the array.
/* accessing elements from the array sequentially */

for( i=0; i<10 ;i++ )
{
Printf(“ %d ”,  number[i]);
}

Explanation:

for loop is used to vary the index of the array from 0 to its size-1.
· The variable used in the for loop is used in any of the executable statement along with the array name enclosed in square brackets.
· In the above example each and every element of the array is accessed and assigned to the variable num and that value is immediately printed.

/* Sample program to illustrate the declaration, initialization and accessing elements of the array */
#include <stdio.h>
void main()
{
int number[10]={1,2,3,4,5,6,7,8,9,10};  /* array declaration and intialization*/
int i; clrscr();
/* printing the elements of the array */
printf(“elements in the array are:\n”);
for(i=0; i<10; i++)
printf(“%d\n”,number[i]);
/* Sample program to illustrate the declaration, reading and printing elements of the array */
#include <stdio.h>
void main()
{
int number[10]  /* array declaration */
int i; clrscr();
/* reading the elements of the array */
printf(“Enter the elements into the array are:\n”);
for(i=0; i<10; i++)
scanf(“ %d ”, &number[i]);
/* printing the elements of the array */
printf(“elements in the array are:\n”);
for(i=0; i<10; i++)
printf(“%d\n”,number[i]);
}
}


Generating a Pointer to an Array


You can generate a pointer to the first element of an array by simply specifying the array name, without any index.
For example, given
int sample[10];


A seven-element character array beginning at location 1000
You can generate a pointer to the first element by using the name sample. Thus, the following
program fragment assigns p the address of the first element of sample:
int *p;
int sample[10];
p = sample;
You can also specify the address of the first element of an array by using the & operator.
Forexample, sample and &sample[0] both produce the same results.
However, in professionally written C code, you will almost never see &sample[0].

Passing Single Dimensional Arrays To Functions:

Arrays can be passed as argument in functions. To pass arrays as arguments one should know how to pass the arguments in function call and how to receive the arguments in the function definition.
The general format to pass an array as argument in function call is given below:
functionname (array name); 


Where array name is the name of the array which is declared in the calling function.
The general format to receive the arguments in the function definition is given below:

<data type> functionname(<data type> arrayname[ ])


Where array name is the name that will be referred in the function definition.
This is used in place of the array name passed from the function call and this array should be declared for its type.

For e.g., int data[10];

we assume that the elements to the array are either initialized or read from the keyboard , then passing the array as argument in the function call is done as shown below:

printelements(data);

the arguments passed from function call are received in the function definition as shown below:

void printelements(int num[ ])

/* sample program illustrating how arrays are passed as arguments in functions */
#include <stdio.h>
void printelements(int [ ]); /* prototype declaration */
void main()
{
          int data[10]; /* array declaration */
int i;
clrscr();
/* reading elements into the array from keyboard */
printf(“enter the elements:”);
for(i=0; i<10; i++)
scanf(“ %d ”, &data[i]);
/* function call */
printelements(data);   /* array passed as argument in function call */
}
/* function definition */
void printelements(int num[ ])    /* actual arguments received in function definition */
{
int i;
printf(“elements are:\n”);
for(i=0; i<10; i++)
printf(“%d\n”,num[i]);