Topic 8 - Arrays Flashcards
Is it legal or illegal for an initializer to be completely empty?
Illegal. must have at least a single 0
What is the notation for multidimensional arrays?
M[i][j]
How does c store arrays?
in “row-wise order” with row 0 first, then row 1 and so forth:
row1-row2-row3
What is the syntax for creating an initializer for 2-dimensional array?
int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 1}};
Can you omit the inner brackets in 2-dimensional array initialization?
Yes. We can even omit the inner braces:
int m[5][9] = {1, 1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 1, 1};
Once the compiler has seen enough values to fill one row, it begins filling the next
• Omittingtheinnerbracescanberisky,sincemissing an element will affect the rest of the initializer
Syntax for designated initializers work with multidimensional arrays:
double identity[2][2] = {[0][0] = 1.0, [1][1] = 1.0};
How do you make an array constant?
An array can be made “constant” by starting its declaration
with the word const:
const unsigned char hex_chars[] = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’};
When is the length of a Variable-Length array computed?
When the program is executed.
Syntax for Variable-Length arrays? Doe it have to be specified by a single variable?
No. int n; int a[n]; int a[3*i+5]; int b[j+k];
What are the 2 restrictions on VLAs:
1) can not have an initializer
2) can not have static storage duration (discussed in ch 18)