array

array := A data structure containing a number of data values, of the same type.

Initialization

int a[10] = {1, 2, 3, 4, 5, 6};
/* inital value of a is {1, 2, 3, 4, 5, 6, 0, 0, 0, 0} */
int a [10] = {0}; // all values in array are 0
int a[10] = {};// WRONG
int a[] = {1,2,3};// implicitly declaring int a[3], and the length is now fixed.

designated initializers in c99

int a[15] = { [2] = 29. [9] = 7, [14] = 48}; // all non-designated (not 2, 9  or 14) indices have value 0.

The order of designators does not matter.

int a[] = {4, 9, 1, 8, [0] = 5, 7} //becomes
int a[] = {5, 7, 1, 8}

2-dimensional arrays

2 dimensional arrays are stored in row-major order, so row 0 is stored first then row 1 etc.

In initialisation you can omit the inner braces.

in c99 you can use

double ident [2][2] = {[0][0] = 1.0, [1][1] = 1.0};

you do not need the inner braces as the compiler can guess what you are trying to do, but it can make your code less clear

int m[5][9] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1,
    1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}

variable length arrays c99

In c99 you can have the length of an array be computed at run time

int a[3*j];

VLAs can be multi-dimensional, they can’t have static storage duration and it can’t have an initializer.

idiom for finding the length of an array

sizeof(a) / sizeof(a[10]);

c flashcard

backlinks