Week 4 - Function Parameters, Pointers, Structures, Linked Lists, Files and Streams Flashcards

1
Q

In C, what happens when you pass a function parameter by value?

A

The value is not updated in the function because a copy is passed rather than the actual variable

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

How do you update the value of a parameter within a function in C?

A

By passing it as a pointer (by reference)

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

In C, can you have a pointer to a pointer?

A

Yes!

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

How do you pass parameter values to main() from the command line in C?

A

int main ( int argc, char **argv )

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

In C, what is argc in this statement int main ( int argc, char **argv )?

A

The number of arguments passed

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

In C, what is argv in this statement: int main ( int argc, char **argv )?

A

An array of string values

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

In C, can you have a pointer to a function?

A

Yes!

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

What is a structure in C?

A

A type of variable that groups multiple related data items together - the closest thing C has to a class

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

How is a structure declared in C?

A

struct <structure>
{</structure>

<variable>
<variable>
};
</variable></variable>

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

When is a structure declared in C?

A

Usually before the function prototypes

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

How can you define your own datatypes in C?

A

typedef

typedef struct <structure> NAME;</structure>

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

What is a linked list? Why are they used?

A

A data structure containing an ordered sequence of nodes. Each node consists of data and one or more links to other nodes to record the sequence.

They are a memory-efficient means of storing large data structures (or where the final size of the large data structure is unknown at compile time), and a speed-efficient means of inserting and deleting nodes.

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

Name 2 advantages of arrays in C.

A

Can go directly to the nth element

Can iterate over them with pointers

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

Name 2 disadvantages of arrays.

A

Insert/delete requires other elements to be moved

When you want to insert a new element into a full array, you either have to stop or extend the array (often by moving the whole thing)

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

Name 4 advantages of linked lists.

A

It does not matter where in memory each node is stored

Insert/delete requires only changing the values of 1 or 2 pointers

Size of linked list is limited only by computer memory

One list may be linked in more than one way

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

Name a disadvantage of a linked list.

A

To find an element, you must start from the beginning and follow the pointers

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

What are the four cases for inserting a new node in a linked list?

A

Add the first node to a new list

Append a node to the end of the list

Insert a node at the beginning of the list

Insert a node into the middle of the list

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

What are the four cases for deleting a node from a linked list?

A

Delete a node from the middle of the list

Delete the node at the beginning of the list

Delete the node at the end of the list

Delete the last node in the list

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

When you delete a node from a linked list in C, what must you always do?

A

Make sure that the memory for deleted nodes is freed (as each node was malloc-ed)

20
Q

Name 4 types of linked lists.

A

Stacks (LIFO)

Queues (FIFO)

Circular lists

Trees

21
Q

In C, how do you take input from the console?

A

scanf()

22
Q

What is a stream?

A

An abstraction of a file that provides a consistent interface to the programmer, regardless of the actual device

It is buffered, may be text or binary, and you need to #include stdio.h

23
Q

What does a stream being buffered mean?

A

Data is not necessarily written to the device when the write command is issued

This can be forced by flushing a stream

24
Q

What does a text stream contain?

A

A sequence of characters (including character translation, e.g. newline -> carriage-return + line-feed

25
Q

What does a binary stream contain?

A

A sequence of bytes (no character translation occurs)

26
Q

To use a stream in C, what header file needs to be included?

A

stdio.h

27
Q

What header file needs to be included to use errno in C?

A

errno.h

28
Q

In C, what does errno do?

A

Value is updated when a new error occurs

You can reset the value

Initial value on program startup is zero

It is a global variable

29
Q

In C, what does ferror(fp) do?

A

Determines whether the most recent operation on the file pointed to by fp produced an error

Returns 1 if an error occurred, 0 if not

Value is reset by each file operation

30
Q

In C, what does the function fputc(ch, fp) do?

A

Outputs the character in ch to the file pointed to by fp

31
Q

In C, what does the function fputs(str, fp) do?

A

Outputs the string in str to the file pointed to by fp

The null terminator is not an output

32
Q

In C, what does the function fprintf(fp, “Character %c, integer %03d”, ch, i) do?

A

Like printf, but the string is output to the file pointed to by fp

33
Q

What are the three ways to write to a stream in C?

A

fputc()
fputs()
fprintf()

34
Q

What are the three ways to read from a stream in C?

A

fgetc()
fgets()
fscanf()

35
Q

In C, what does the function fgetc(fp) do?

A

Reads a single character from the file pointed to by fp and stores it in ch

36
Q

In C, what does the function fgets(str, count, fp) do?

A

Reads multiple characters up to an including the next newline character (or a maximum of count characters) from the file pointed to by fp and stores it in the string str

Adds a null terminator to str

Cannot distinguish between null character read in and the terminating null character

37
Q

In C, what does the function fscanf(fp, “%d, %d”, &x, &y) do?

A

Like scanf, but the string is input from the file pointed to by fp

38
Q

In C, what does the function fflush(fp) do?

A

Forces the output buffer to be written to the file

39
Q

In C, what does the function remove(“myFile.txt”) do?

A

Deletes the specified file, returning 0 on success, other integer on fail

40
Q

In C, what does the function rewind(fp) do?

A

Resets the file position indicator back to the beginning of the file

41
Q

In C, what does the function sprintf(str, “Character %c, integer %03d”, ch, i) do?

A

Like fprintf, but the output string is stored in the variable str

42
Q

In C, what does the function sscanf(str, “%d, %d”, &x, &y) do?

A

Like fscanf, but the string is input from the variable str

43
Q

What are the three standard streams in C that all programs can use without calling fopen or fclose?

A

stdin (input from the terminal)

stdout (output to the terminal; normal program output)

stderr (output to the terminal; error and warning messages)

44
Q

When are streams flushed?

A

At the end of each line (‘\n’) when output to a terminal

When the buffer is full

45
Q

What is a common buffer size?

A

8192 bytes

46
Q

How can ‘raw’ (unbuffered) read and write be achieved?

A

read() and write()