5.1 Arrays

What is the first index of every array in C?
The first index of every array in C is 0.

What does the name of an array refer to?
The name of an array refers to the memory address at which its storage begins.

How are the elements of an array stored in memory?
In memory, the elements of an array are stored contiguously.

What is the memory address of the second element if the first element of an array is stored at memory address ?
If the first element of an array is stored at memory address , the memory address of the second element is .

5.1.1 Array initialization

What is the contents of an array when it is initialized?
When an array is initialized, the contents of it are undefined.

What are three common practices for setting the values of an array to an initial value?
Three common practices for setting the values of an array to an initial value are:

  1. Using a for loop to set all values to something specific.
  2. Using the memset or bzero functions to set all values to 0.
  3. Using initializer syntax.

In which header file is the memset function declared?
The file where the memset function is declared is in strings.h.

What are the three parameters of the memset function?
The three parameters of the memset function are:

  1. A memory address.
  2. The character that should be written into each byte of the memory address.
  3. The number of bytes to be set.

What should you use when setting the parameter for the number of bytes to be set with the memset?
When setting the parameter for the number of bytes to be set with memset, you should use sizeof(data_type) * array_length.

Example of using a for loop to initialize an array's values
int scores[100];
for (int i = 0; i < 100; i++) {
	scores[i] = 0;
}
Example of using the memset function to initialize an array's values
#include <string.h>
int scores[100];
memset(scores, 0, 100 * sizeof(int)); /* or */
memset(scores, 0, sizeof(scores));
Example of using initializer syntax to initialize an array's values
int scores[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

...

5.1.3 No array bounds checking!

What happens if you try to access an array index which is out of bounds?
If you try to access an array index which is out of bounds, the behavior is undefined.

What is the best way to prevent out of bounds errors?
The best way to prevent out of bounds errors is to use tools for detecting memory corruption.

What are two tools for detecting memory corruption in your program?
Two tools for detecting memory corruption in your program are:

  1. valgrind.
  2. The clang static analyzer.

...

5.1.4 Variable length arrays

What is an array called when you specify the size of it with a variable?
When you specify the size of an array with a variable, it is called a variable length array.

What C standard allowed variable length arrays?
The C standard that allowed variable length arrays was C99.

What should you make sure of if there is a compile-time error on a line that declares a variable length array?
If there is a compile-time error on a line that declares a variable length array, you should make sure that the compiler is set to use the C99 standard with -std=c99.

5.2 Multidimensional arrays

How do you declare a multidimensional array in C?
To declare a multidimensional array in C, you use multiple pairs of square braces in the declaration of the array variable.

Can you use the initializer syntax to initialize a multidimensional array?
Yes, you can use the initializer syntax to initialize a multidimensional array.

Example of declaring a multidimensional array
char board[3][3];
Example of declaring and initializing a multidimensional array using the initializer syntax
char board[3][3] = {{'O', 'O', ' '},
					{'X', 'X', 'O'},
					{' ', 'O', 'X'}};

How is a multidimensional array stored in memory?
In memory, a multidimensional array is stored in a single continuous block of memory in row-major order.

What is row-major order?
Row-major order is when the rightmost element of a subarray is next to the leftmost element of the next subarray.

5.3 C Strings

How much support does C have for strings?
The support that C has for strings is minimal.

What is a string in C?
In C, a string is an array of characters with a null character at the end.

...