Introduction to Arrays

Declaring Arrays:

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows:

type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type.

For example, to declare a 10-element array called balance of type double, use this statement:
double balance[10];

Now balance is avariable array which is sufficient to hold upto 10 double numbers.
Initializing Arrays:

You can initialize array in C either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};

The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ].

Following is an example to assign a single element of the array:

If you omit the size of the array, an array just big enough to hold the initialization is created.

Therefore, if you write:

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};

You will create exactly the same array as you did in the previous example.

balance[4] = 50.0;

Multi-dimensional Arrays

Here is the general form of a multidimensional array declaration:

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array:
int threedim[5][10][4];

Two-Dimensional Arrays:

The simplest form of the multidimensional array is the two-dimensional array.

A two-dimensional array is, in essence, a list of one-dimensional arrays.

To declare a two-dimensional integer array of size x,y you would write something as follows:
type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier. A two dimensional array can be think as a table which will have x number of rows and y number of columns.

A 2-dimentional array a which contains three rows and four columns can be shown as below:

Two Dimensional Arrays in C

Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

Initializing Two-Dimensional Arrays:

Multidimensionalarrays may be initialized by specifying bracketed values for each row.

Following is an array with 3 rows and each row have 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */

The nested braces, which indicate the intended row, are optional.

The following initialization is equivalent to previous example:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Pointer to an Array

An array name is a constant pointer to the first element of the array. Therefore, in the declaration:

double balance[50];

balance is a pointer to &balance[0], which is the address of the first element of the array balance.

Thus, the following program fragment assigns p the address of the first element of balance:

double *p;
double balance[10];
p = balance;

It is legal to use array names as constant pointers, and vice versa.

Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4].
Once you store the address of first element in p, you can access array elements using *p, *(p+1), *(p+2) and so on.

Below is the example to show all the concepts discussed above:

#include < stdio.h >
int main ()
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ )
printf("*(p + %d) : %f\n", i, *(p + i) );
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ )
printf("*(balance + %d) : %f\n", i, *(balance + i) );
return 0;

Share this

Related Posts

Dear User,

Thank you for your comment. Hope you like this blog. Kindly share us on Social Media so others can be updated.

-Chief Administrative Officer.