Lecture 3 Flashcards

Arrays and Strings

1
Q

for a single-dimensional array, can you have different data types?

A

no, all elements must be the same type
ex) int array –> only ints allowed

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

what is the sequence of elements in a single dimensional array?

A

contiguous aka ordered sequence
- no holes in the array, must be
elements at every index

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

is the size/length of an array fixed?

A

yes, the number of elements (N) is set when the array is created
ex) int a[5];

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

what index position is the first element in the array located at?

A

A[0]

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

what index position is the last element in the array located at?

A

A[N - 1], where N is the number of elements

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

single-dimensional array basic syntax

A

data_type name[size];
ex) int mol[5];

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

if you don’t initialize your array with values, will it be assigned to 0?

A

no, it will be assigned to whatever is leftover in memory at those address locations
**best to initialize upon creation

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

what happens in memory when you create an array and initialize it with values?

A

the right amount of memory will be allocated based on the size of the array and will be set to those values

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

how do you calculate how many bytes are allocated on the stack for an array?

A

number of elements (size) * size of data type
ex) int a[5]
int = 4 bytes
size = 5 elements
4 * 5 = 20 bytes allocated

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

how to find how many bytes are allocated on the stack for an array using code?

A

using sizeof( )
ex) int A[N];
int bytes = N * sizeof(int);

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

does c have a length property?

A

no

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

how to find the size of an array?

A

size = total # of bytes / size of data type
ex)
int size = sizeof(A) / sizeof(int);
- this returns the total number of bytes divided by the amount of bytes for each element –> gets size of array

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

is there array bound checking in c?

A

no

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

if you try to access an index in your array outside of the size of your array (int A[5]; –> say something like A[10]), what happens?

A

unpredictable behavior: may work, but a memory fault (aka segmentation fault) will likely occur
- memory access violation –> attempting to access a restricted area in memory

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

multi-dimensional array basic syntax

A

data_type name [size1] [size2]…size[N];

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

int A [2] [3];
how many rows and columns are there?

A

2 rows, 3 columns

17
Q

how to calculate the number of bytes allocated in memory for multi-dimensional array?

A

of bytes = # of elements * sizeof(data type)

18
Q

how to calculate the number of elements in a multi-dimensional array?

A

array[size1] [size2];
size1 * size2

19
Q

how to calculate the number of rows in a multidimensional array?

A

number of rows = total # of bytes / # of bytes in one row

ex)
int A[2] [3];
int rows = sizeof(A) / sizeof(A[0]);

20
Q

how to calculate the number of columns in a multidimensional array?

A

number of columns = # of bytes in one row / # of bytes in one element
ex)
int columns = sizeof(A[0]) / sizeof(int);

21
Q

is there a string data-type in c?

A

no, just a char array

22
Q

what must all char arrays be?

A

null terminated

23
Q

what is the null terminator?

A

‘\0’
- marks the end of the string
- also equal to integer 0

24
Q

what happens to characters after the null terminator in the array?

A

they are considered invalid, would not get printed if displayed on the console

25
Q

char string[5] = “abcd”;
char string[] = “abcd”;
char string[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘\0’}

which of the following are correct implementations?

A

all of them

26
Q

if you initialize this way:
char string[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘\0’}
do you have to include the null terminator?

A

YES, all other ways include it for you, but must add it if you initialize this way

27
Q

what is string.h?

A

string library, gives you access to all the functions in this string library

28
Q

what is strlen() ?

A

function included in the string library
- determines number of characters
- counts characters UP to null terminator

29
Q

does strlen() include the null terminator in it’s count?

A

NOO, counts characters up until null terminator

30
Q

char string[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘\0’};
printf(“%d\n”, strlen(string));

what is printed?

A

4
- abcd = 4 characters
- counts up until null terminator

31
Q

char string[5] = {‘a’, ‘b’, ‘c’, ‘d’, ‘\0’};
string[2] = ‘\0’;
printf(“%d\n”, strlen(string));

what is printed?

A

2
- ab = 2 characters
- counts up until null terminator @ index 2
- doesn’t include it in count or any char after it

32
Q

char string1[] = “brent”;
char string2[] = “david”;
string1 = string2;

does this work?

A

no, both of these variables point to a memory address –> will cause error
char arrays point to strings

33
Q

char string1[] = “brent”;
char string2[] = “david”;
strcpy(string2, string1);

what does strcpy do?

A

copies character by character from string1 into string2
string2 now equals “brent”