tags:
- programming
- c
- book
- computer-science
- notes
source: https://jsommers.github.io/cbook/cbook.pdf
created: 2024-12-04
published: 2022-08-01
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
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:
- Using a
for
loop to set all values to something specific.- Using the
memset
orbzero
functions to set all values to 0.- Using initializer syntax.
In which header file is the
memset
function declared?
The file where thememset
function is declared is instrings.h
.What are the three parameters of the
memset
function?
The three parameters of thememset
function are:
- A memory address.
- The character that should be written into each byte of the memory address.
- 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 withmemset
, you should usesizeof(data_type) * array_length
.
for
loop to initialize an array's valuesint scores[100];
for (int i = 0; i < 100; i++) {
scores[i] = 0;
}
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));
int scores[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
...
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:
valgrind
.- The
clang
static analyzer.
...
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
.
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.
char board[3][3];
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.
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.
...