### Examples of Array Declaration and Use

The following show four different character array initializations:

>     static char name1[] = { 'J', 'a', 'n' };
>     static char name2[] = { "Jan" };
>     static char name3[3] = "Jan";
>     static char name4[4] = "Jan";

These initializations create the following elements:

name1 | name2 | name3 | name4
Element | Value | Element | Value | Element | Value | Element | Value
--- | --- | --- | --- | --- | --- | --- | ---
`name1[0]` | `J` | `name2[0]` | `J` | `name3[0]` | `J` | `name4[0]` | `J`
`name1[1]` | `a` | `name2[1]` | `a` | `name3[1]` | `a` | `name4[1]` | `a`
`name1[2]` | `n` | `name2[2]` | `n` | `name3[2]` | `n` | `name4[2]` | `n`
`` | `` | `name2[3]` | `\0` | `` | `` | `name4[3]` | `\0`

Note that the `NULL` character (\0)is lost for `name1[]` and `name3[3]`. A compiler warning is issued for `name3[3]`.

The following program defines a floating-point array called `prices`.

The first for statement prints the values of the elements of `prices`. The second `for` statement adds five percent to the value of each element of `prices`, and assigns the result to `total`, and prints the value of `total`.

>     /**
>      ** Example of one-dimensional arrays
>      **/

>     #include <stdio.h>
>     #define  ARR_SIZE  5

>     int main(void)
>     {
>       static float const prices[ARR_SIZE] = { 1.41, 1.50, 3.75, 5.00, .86 };
>       auto float total;
>       int i;

>       for (i = 0; i < ARR_SIZE; i++)
>       {
>         printf("price = $%.2f\n", prices[i]);
>       }

>       printf("\n");

>       for (i = 0; i < ARR_SIZE; i++)
>       {
>         total = prices[i] * 1.05;

>         printf("total = $%.2f\n", total);
>       }

>       return(0);
>     }

This program produces the following output:

>     price = $1.41
>     price = $1.50
>     price = $3.75
>     price = $5.00
>     price = $0.86

>     total = $1.48
>     total = $1.57
>     total = $3.94
>     total = $5.25
>     total = $0.90

The following program defines the multidimensional array `salary_tbl`. A for loop prints the values of `salary_tbl`.

>     /**
>      ** Example of a multidimensional array
>      **/

>     #include <stdio.h>
>     #define  ROW_SIZE     3
>     #define  COLUMN_SIZE  5

>     int main(void)
>     {
>       static int salary_tbl[ROW_SIZE][COLUMN_SIZE] =
>       {
>         {  500,  550,  600,  650,  700   },
>         {  600,  670,  740,  810,  880   },
>         {  740,  840,  940, 1040, 1140   }
>       };
>       int grade , step;

>       for (grade = 0; grade < ROW_SIZE; ++grade)
>        for (step = 0; step < COLUMN_SIZE; ++step)
>        {
>          printf("salary_tbl[%d] [%d] = %d\n", grade, step,
>                  salary_tbl[grade] [step]);
>        }

>        return(0);
>     }

This program produces the following output:

>     salary_tbl[0] [0] = 500
>     salary_tbl[0] [1] = 550
>     salary_tbl[0] [2] = 600
>     salary_tbl[0] [3] = 650
>     salary_tbl[0] [4] = 700
>     salary_tbl[1] [0] = 600
>     salary_tbl[1] [1] = 670
>     salary_tbl[1] [2] = 740
>     salary_tbl[1] [3] = 810
>     salary_tbl[1] [4] = 880
>     salary_tbl[2] [0] = 740
>     salary_tbl[2] [1] = 840
>     salary_tbl[2] [2] = 940
>     salary_tbl[2] [3] = 1040
>     salary_tbl[2] [4] = 1140

Array Type