C++ Primer - Chapter 3 (Strings, Vectors & Arrays) Flashcards
What is an array?
A container of unnamed objects of a single type that we access by position.
Can arrays be doubled in size?
No. Arrays cannot be resized at all; they have fixed size.
True or false? The dimension of an array is not a part of its type.
False. The dimension of an array is part of its type (and must be known at compile-time).
What is the issue with the following code?
int sz=4;
int arr[sz]={0,1,2,3};
int sz=4; int arr[sz]={0,1,2,3}; // Error; the dimension of an array must be a constant expression so that it can be known at compile-time.
If the size of the container that is required is unknown to the user, which is better to use? An array or a vector?
A vector, as it can be resized after it has been created.
Are there any issues with the following code?
int *parray[10]={};
int &rarray[10]={};
int *parray[10]={}; int &rarray[10]={}; // This is an error; an array can only hold objects. Thus, an array cannot hold a reference.
Fill in the blank:
Like pointers and references, arrays are a ________.
Like pointers and references, arrays are a compound type.
Describe the following code. Are there any errors? const unsigned sz = 3; int ia1[sz] = {0,1,2}; int a2[] = {0, 1, 2}; int a3[5] = {0, 1, 2}; string a4[3] = {"hi", "bye"}; int a5[2] = {0,1,2};
const unsigned sz = 3; int ia1[sz] = {0,1,2}; // ia1 is an array of three ints with values 0, 1, 2. int a2[] = {0, 1, 2}; // a2 is an array of dimension 3. int a3[5] = {0, 1, 2}; // This is equivalent to writing a3[] = {0, 1, 2, 0, 0}. string a4[3] = {"hi", "bye"}; // This is the same as a4[] = {"hi", "bye", ""}. int a5[2] = {0,1,2}; // This is an error; too many initializers.
What is the difference between the two arrays below?
char a1[] = {’C’, ’+’, ’+’};
char a2[] = “C++”;
char a1[] = {’C’, ’+’, ’+’}; // This contains no null character for termination. char a2[] = "C++"; // A null terminator is added automatically because a2 is created from a string.
Is it possible to copy or assign an array?
No. We cannot initialise an array as a copy of another array and one array cannot be assigned to another.
What does the following code do? int arr[10]; int *ptrs[10]; int &refs[10] = /* ... */; int (*Parray)[10] = &arr; int (&arrRef)[10] = arr;
int arr[10]; int *ptrs[10]; // ptrs is an array of ten pointers to int. int &refs[10] = /* ... */; // Error: there are no arrays of references. int (*Parray)[10] = &arr; // Parray points to an array of ten ints. int (&arrRef)[10] = arr; // arrRef refers to an array of ten ints.
What is arry in the following code?
int *(&arry)[10] = ptrs;
int *(&arry)[10] = ptrs; // arry is a reference to an array of 10 pointers to ints.
Assuming txt_size is a function that takes no arguments and returns an int value, which of the following definitions are illegal? Explain why. unsigned buf_size = 1024; (a) int ia[buf_size]; (b) int ia[4 * 7 - 14]; (c) int ia[txt_size()]; (d) char st[3] = "fun";
unsigned buf_size = 1024; (a) int ia[buf_size]; // This is an error; buf_size is not a constant expression. (b) int ia[4 * 7 - 14]; // This is okay; 4 * 7 - 14 is a constant expression. (c) int ia[txt_size()]; // This is okay if txt_size() is constexpr. (d) char st[3] = "fun"; // This is an error; this should be an array of size 4 as string literals end with a (hidden) null character.
What are the values in the following arrays? std::string sa[10]; int ia[10]; int main() { std::string sa2[10]; int ia2[10]; }
std::string sa[10]; // Default initialised to an array of empty strings (as it is outside a function). int ia[10]; // Default initialised to an array of zeros (as it is outside a function). int main() { std::string sa2[10]; // Array of 10 strings of empty strings. int ia2[10]; // Array of 10 ints with uninitialised entries. }
List up to five of the drawbacks of using an array instead of a vector.
- The size of the array is fixed and must be known when defined.
- A character array must leave space for null character at the end.
- Arrays can not be copied or assigned as a whole.
- Array can not be constructed with the same values like the constructor of vector when defined.
- The size of array must be calculated (sometimes it cannot be calculated at all) instead of calling a size() method.
What is the problem with the following code?
auto ia1[3] = {0,1,2};
auto ia1[3] = {0,1,2}; // Error; we cannot use auto to deduce the type from a list of initializers.
What are two ways to loop over the entries of an array?
- With a regular for-loop (e.g. using the known size), or;
2. With a range-for (e.g. with the type auto deduced).
Identify the indexing errors in the following code:
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ++ix)
ia[ix] = ix;
constexpr size_t array_size = 10; int ia[array_size]; for (size_t ix = 1; ix <= array_size; ++ix) // Error; accesses the off-the-end element of the array. It is an error to access this entry. ia[ix] = ix;
What type does ia2 have in the following code? int ia[] = {0,1,2,3,4,5,6,7,8,9}; auto ia2(ia);
int ia[] = {0,1,2,3,4,5,6,7,8,9}; auto ia2(ia); // ia2 is an int* that points to the first element in ia.