C Data Structures and Pointers
- 68Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is a pointer in C?
A variable that stores the address of a location in memory.
Or swipe the card
68 cards
What is a pointer in C?
A variable that stores the address of a location in memory.
What does
int *xdeclare?A pointer named
xthat can store the address of an integer.What does
mallocdo, and what does it return?It allocates a requested number of bytes and returns the address of the allocated memory.
How can you request enough memory for one integer without hard-coding its byte size?
Use
malloc(sizeof(int)).What does the dereference operator
*do when applied to a pointer?It accesses the value stored at the memory address the pointer contains.
Why is dereferencing an uninitialized pointer unsafe?
It may contain an unknown address, so accessing it can cause invalid memory access or a segmentation fault.
If
y = x, what happens to the pointers and their pointee?yreceives the same address asx; both pointers refer to the same memory, which is not copied.What happens if
*x = 42is followed by*y = 13whenxandypoint to the same location?The shared value becomes 13; the assignment overwrites 42.
What should a program check after calling
malloc?Whether the returned pointer is
NULL, which indicates allocation failed.What does
free(p)do?It releases the dynamically allocated memory that
ppoints to.How are
NULLand the character\0different in C?NULLis a null pointer value;\0is the null character. Both are represented by zero underneath.Why can allocated memory become a memory leak?
If a program loses every pointer to allocated memory, it can no longer access or free that memory.
Why does a fixed-size array make adding an unanticipated element difficult?
Its size is set when it is created, and adjacent memory may already be in use.
How can a program grow an array when the adjacent memory is unavailable?
Allocate a larger block elsewhere, copy the existing elements, release the old block, and use the new block.
What is the worst-case time complexity of growing an array by copying its existing elements?
O(n).
What is the time complexity of accessing an array element by index?
O(1).
What search method can search a sorted array in O(logn) time?
Binary search.
How can
mallocallocate space for three integers?Use
malloc(3 * sizeof(int))and store the returned address in an integer pointer.Why can square-bracket indexing be used with memory allocated by
malloc?The allocated block is contiguous, so pointer arithmetic can reach each element using array-style indexing.
In a block of integers, how far apart are consecutive indexed elements in memory?
They are separated by
sizeof(int)bytes; if an integer is 4 bytes, each index advances 4 bytes.What does
reallocdo?It resizes an existing allocated block, growing or shrinking it, and handles preserving or moving its contents.
Why should the result of
reallocoften be stored in a temporary pointer?If reallocation fails and returns
NULL, the original pointer is still available to the program.Which header file provides
malloc,free, andreallocin C?stdlib.h.What is a node in a linked list?
A unit of memory that stores a value and a pointer linking it to another node.
What is a linked list?
A sequence of nodes connected by pointers, which may be stored in noncontiguous memory.
What fields might a node in a linked list of integers contain?
An integer value and a pointer to the next node.
Why must a self-referential C structure use
struct node *nextrather than anode *nextalias inside its own definition?The
nodetypedef alias is not available until the structure definition is complete.How is an empty linked list represented?
By a list pointer set to
NULL.What must be allocated to add an integer to a linked list?
A whole node containing space for both the integer and its pointer field.
Why is a newly allocated node's
nextfield often set toNULL?It marks that there is currently no next node, such as when the new node is at the end of the list.
What does the C expression
n->numbermean?Access the
numberfield of the node pointed to byn.How can a program traverse a linked list to find its end?
Start at the head and repeatedly follow
nextpointers until the current node'snextisNULL.When inserting a node at the beginning of a linked list, what pointer updates preserve the existing list?
Set the new node's
nextto the old head, then set the head to the new node.When inserting a node between two linked-list nodes, which link should be set first?
Set the new node's
nextto the node that should follow it, then link the preceding node to the new node.What is the worst-case search time for a linked list?
O(n).
Why can't a linked list efficiently use binary search, even if its values are sorted?
It lacks random access to the middle element; reaching a position requires following links from the head.
What is the worst-case time to insert into a sorted linked list when the correct position must first be found?
O(n).
What is the main advantage of linked lists over fixed-size arrays when adding elements?
They can grow by allocating new nodes without moving the existing nodes.
What is the main cost of linked lists compared with arrays?
They use extra memory for pointers and lose constant-time indexed access.
What does a linked list's head pointer need to identify the entire list?
The address of the first node.
What is a binary tree?
A tree in which each node has at most two children.
What ordering property defines a binary search tree?
For each node, values in its left subtree are smaller and values in its right subtree are larger.
What pointers does a binary search tree node typically need?
A
leftpointer and arightpointer, plus its stored value.What is the root of a tree?
The topmost starting node from which the rest of the tree can be reached.
How does searching a binary search tree decide which subtree to inspect?
Compare the target with the current node: go left if it is smaller and right if it is larger.
What is the base case when recursively searching a binary search tree?
If the current tree pointer is
NULL, the target is not present, so return false.What is the search time in a balanced binary search tree?
O(logn).
Why can an unbalanced binary search tree have poor search performance?
It can become very tall, making searches take up to O(n) time.
How can a binary search tree retain logarithmic performance as values are inserted?
Use balancing algorithms that rearrange links to keep the tree's height controlled.
What is a hash function?
A function that takes an input and produces an output used to determine where that input is stored.
What is a hash table?
An array of buckets indexed by a hash function, often with a linked list at each bucket to handle collisions.
What is a collision in a hash table?
When multiple inputs map to the same bucket.
How can a hash table resolve collisions using linked lists?
Store colliding values in a linked list attached to their shared bucket.
Why can a hash table have O(n) worst-case search time?
All values could collide into one bucket, creating a chain that must be searched linearly.
How can using more characters from a name in a hash function affect collisions?
It can reduce collisions by spreading names across more buckets, but requires a larger table and does not guarantee no collisions.
What trade-off does a hash table illustrate?
Using more memory or a more sophisticated hash function can improve lookup speed and reduce collisions.
What is a trie?
A tree-like structure whose nodes represent arrays of possible next characters.
How is a word stored in a trie?
Follow or create one branch for each character in order, then mark the node at the word's end.
Why can a trie provide constant-time lookup with respect to the number of stored words?
Lookup follows only the characters of the target word, so its work depends on word length rather than the number of stored words.
What is the main cost of using a trie?
High memory usage, because each node may contain an array of possible character branches.
What does FIFO mean for a queue?
First in, first out: the earliest item added is the first item removed.
What are the two basic queue operations?
Enqueueadds an item;dequeueremoves an item.What does LIFO mean for a stack?
Last in, first out: the most recently added item is the first removed.
What are the two basic stack operations?
Pushadds an item to the top;popremoves the top item.How do a queue and a stack differ in removal order?
A queue removes the oldest item first (FIFO); a stack removes the newest item first (LIFO).
What is a dictionary as an abstract data structure?
A structure that maps keys to values, such as words to definitions or names to records.
How can a hash table support a dictionary?
Use each key as input to a hash function to locate the bucket containing its associated value.
Why might a hash-based name-tag or salad pickup system be faster than searching one large stack?
It directs a person to a bucket or section based on a key, reducing the amount of searching required.

Ask anything
We've read this whole study set, so questions about it get the best answers.