Home > Electronics Tutorials > C Language Tutorial > Identifiers - Arrays

C Language Programming Library Reference Guide

Identifiers - Arrays

Arrays create single or multidimensional matrices. They are defined by appending an integer encapsulated in brackets at the end of a variable name. Each additional set of brackets defines an additional dimension to the array. When addressing an index in the array, indexing begins at 0 and ends at 1 less than the defined array. If no initial value is given to the array size, then the size is determined by the initializers. When defining a multidimensional array, nested curly braces can be used to specify which dimension of the array to initialize. The outermost nest of curly braces defines the leftmost dimension, and works from left to right.

Examples:

int x[5];
Defines 5 integers starting at x[0], and ending at x[4]. Their values are undefined.

char str[16]="Blueberry";
Creates a string. The value at str[8] is the character "y". The value at str[9] is the null character. The values from str[10] to str[15] are undefined.

char s[]="abc";
Dimensions the array to 4 (just long enough to hold the string plus a null character), and stores the string in the array.

int y[3]={4};
Sets the value of y[0] to 4 and y[1] and y[2] to 0.

 
int joe[4][5]={
     {1,2,3,4,5},
     {6,7,8,9,10},
     {11,12,13,14,15}
};
The first row initializes joe[0], the second row joe[1] and so forth. joe[3] is initialized to 5 zeros.

The same effect is achieved by:
int joe[4][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......