Arrays and Pointers Flashcards

1
Q

How is an array like a struct?

A

Both are aggregate data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

T/F: Arrays can contain more than one type.

A

F; the types need to be uniform.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When adding the subscript operator ([]) to a variable declaration, what is the compiler being told to do?

A

Set aside contiguous blocks of memory (the number of blocks is given inside []), with each block of the data type’s size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

T/F: When declaring an array, the size of the array must be a constant.

A

T

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Will the following code compile?

int nSize = 5;
int anArray[nSize];

A

No; ‘nSize’ must be const qualified for this to work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the syntax to access the member of a struct within an array?

A

arrayName[int index].memberName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between the following two declarations:

int anArray[5] = { 3, 2, 7, 5, 8 };

int anArray[5] = { 0 };

A

The first defines each element of the array with an initializer list, and the second sets all 5 elements of the array to 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Will the following code compile?

int anArray[] = { 0, 1, 2, 3, 4 };

A

Yes; the compiler can extrapolate the size of the array from the length of the initializer list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can sizeof() be used to get the number of elements in an array?

A

Divide the total allocated space by the size of a single element:

int nElements = sizeof(anArray) / sizeof(anArray[0]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use enums to index into an array?

A

To avoid magic numbers:

enum StudentNames
{
    KENNY, // 0
    KYLE, // 1
    STAN, // 2
    BUTTERS, // 3
    CARTMAN, // 4
    WENDY, // 5
    MAX_STUDENTS // 6
};

int anTestScores[MAX_STUDENTS]; // allocate 6 integers
anTestScores[STAN] = 76;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the syntax to specify an array as a parameter?

A

typeName arrayName[]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does swap() do, and what is its library?

A

In swap(x, y), the values of the variables ‘x’ and ‘y’ are swapped. swap() is in the ‘algorithm’ library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Articulate the steps of a selection sort.

A

1) Starting at index 0, search the entire array to find the smallest value
2) Swap the smallest value found with the value at index 0
3) Repeat steps 1 & 2 starting from the next index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the best way to initialize a multidimensional array?

A

With nested braces:

int anArray[3][5] =
{
{ 1, 2, 3, 4, 5, },
{ 6, 7, 8, 9, 10, },
{ 11, 12, 13, 14, 15 }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T/F, you can leave out the first dimension but not the second when declaring a multidimensional array.

A

T; The compiler can figure out the first dimension but not the others (it ignores braces, so it cannot intuitively figure out the array’s dimensions; it only sees a string of digits).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the length of the following C-style string:

char str[] = “string”;

A

7; the last is the ‘null’ character (ASCII code 0).

17
Q

What is a buffer?

A

A buffer is memory set aside temporarily to hold data.

18
Q

What is buffer overflow?

A

A buffer overflow occurs when the program tries to store more data in a buffer than the buffer can hold. Buffer overflow results in other memory being overwritten, which usually causes a program crash, but can cause any number of other issues.

19
Q

What is an advantage of using the ‘string’ library’s getline() function?

A

It reads in strings including whitespace:

cout &laquo_space;“Enter your full name: “;

string strName;
getline(cin, strName);

cout &laquo_space;“You entered: “&laquo_space;strName «endl;

20
Q

Explain the following declaration:

int *pnPtr = 0;

A

We are declaring a pointer and initializing it as a null pointer.

21
Q

How can a pointer be used as a boolean?

A

A null (or 0) pointer will evaluate to false. Remember that any value other than 0 is true!

if (pnPtr)
cout &laquo_space;“pnPtr is pointing to an integer.”;
else
cout &laquo_space;“pnPtr is a null pointer.”;

22
Q

What determines the size (in memory ) of a pointer (not its value)?

A

The architecture of the computer: 32 bit will have a 4 byte addressing scheme with 4 byte pointers, 64 bit will have 8, and so on.

23
Q

T/F: C-style strings follow all the same rules as arrays.

A

T; and they should be treated identically.