Week 4 - Function Parameters, Pointers, Structures, Linked Lists, Files and Streams Flashcards
In C, what happens when you pass a function parameter by value?
The value is not updated in the function because a copy is passed rather than the actual variable
How do you update the value of a parameter within a function in C?
By passing it as a pointer (by reference)
In C, can you have a pointer to a pointer?
Yes!
How do you pass parameter values to main() from the command line in C?
int main ( int argc, char **argv )
In C, what is argc in this statement int main ( int argc, char **argv )?
The number of arguments passed
In C, what is argv in this statement: int main ( int argc, char **argv )?
An array of string values
In C, can you have a pointer to a function?
Yes!
What is a structure in C?
A type of variable that groups multiple related data items together - the closest thing C has to a class
How is a structure declared in C?
struct <structure>
{</structure>
<variable>
<variable>
};
</variable></variable>
When is a structure declared in C?
Usually before the function prototypes
How can you define your own datatypes in C?
typedef
typedef struct <structure> NAME;</structure>
What is a linked list? Why are they used?
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.
Name 2 advantages of arrays in C.
Can go directly to the nth element
Can iterate over them with pointers
Name 2 disadvantages of arrays.
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)
Name 4 advantages of linked lists.
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
Name a disadvantage of a linked list.
To find an element, you must start from the beginning and follow the pointers
What are the four cases for inserting a new node in a linked list?
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
What are the four cases for deleting a node from a linked list?
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