C Data Structures and Pointers
- 75Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is a pointer in C, and what does it store?
75 questions
- 1
What is a pointer in C, and what does it store?
Model answer
A pointer is a variable that stores the address of a location in memory, such as the address of an integer.
Explanation
Pointers let a program refer to memory locations by their addresses.
- 2
What does
mallocdo, and what does it return?Model answer
mallocallocates a requested number of bytes and returns the address of the allocated memory.Explanation
The returned address is typically stored in a pointer.
- 3
How can
sizeofhelp when allocating memory for an integer?Model answer
sizeof(int)gives the number of bytes needed for an integer, so it can be passed tomallocto request enough memory.Explanation
Using
sizeofavoids assuming that an integer always occupies a particular number of bytes. - 4
What does the dereference operator
*do when applied to a valid pointer?Model answer
It accesses the value at the memory address stored in the pointer, allowing that value to be read or changed.
Explanation
For example, assigning
42through a pointer stores the value at the pointed-to address. - 5
Why is dereferencing a pointer that has not been initialized unsafe?
Model answer
An uninitialized pointer may contain an unknown address, so dereferencing it can access invalid memory and cause a fault.
Explanation
A pointer should refer to valid allocated memory before the program uses the address it contains.
- 6
What happens when one pointer is assigned the value of another pointer?
Model answer
Both pointers contain the same address and therefore refer to the same memory location.
Explanation
Assigning a pointer copies its address; it does not allocate a separate pointee.
- 7
If two pointers refer to the same integer, what happens when one pointer changes that integer?
Model answer
The change is visible through both pointers because they refer to the same memory location.
Explanation
Writing through either pointer updates the shared pointee.
- 8
What is the difference between
NULLand the character literal\0in the context described?Model answer
NULLrepresents a null pointer, while\0is the null character; both have an underlying value of zero but represent different kinds of things.Explanation
The content distinguishes a pointer value from a character value even though both are associated with zero.
- 9
What should a program check after calling a memory-allocation function such as
malloc?Model answer
It should check whether the returned pointer is
NULL, which indicates that allocation failed.Explanation
Continuing to use a null result as though it pointed to allocated memory is unsafe.
- 10
What does
freedo, and when should it be used?Model answer
freereleases dynamically allocated memory when the program is done using it.Explanation
The program passes the address of the allocated memory to
free. - 11
What is a data structure?
Model answer
A data structure is a programming construct for organizing and storing information in computer memory.
Explanation
Different data structures arrange information differently to support different operations.
- 12
What roles do
struct, the dot operator, and the dereference operator play in C data structures?Model answer
structdefines a structure containing fields, the dot operator accesses a field of a structure value, and*dereferences a pointer to access the memory it points to.Explanation
These features provide building blocks for defining and working with more complex data structures.
- 13
Why can a fixed-size array not simply grow into the memory immediately after it?
Model answer
Other data may occupy the neighboring memory, so there is no guarantee that the space after the array is available.
Explanation
An array occupies a contiguous region, but its surroundings may already be in use.
- 14
What must a program do to grow an array when the required next element will not fit?
Model answer
It must allocate a larger memory region, copy the existing elements into it, release the old region, and then use the larger region.
Explanation
The new region may be elsewhere in memory rather than directly adjacent to the original array.
- 15
Why does copying an array of n elements to a larger allocation take O(n) time?
Model answer
The program must copy each of the n existing elements, so the work grows in proportion to the array's length.
Explanation
The copying takes a constant amount of work per element.
- 16
How does
reallocsimplify resizing a dynamically allocated array?Model answer
reallocresizes an existing allocation and handles moving and copying its contents if necessary.Explanation
The program still needs to check the result and keep track of the returned pointer.
- 17
Why should a program save the result of
reallocin a temporary pointer before replacing the original pointer?Model answer
If the result is assigned directly to the original pointer and resizing fails, the original address may be lost, leaving the old allocation inaccessible.
Explanation
A temporary pointer preserves the original address while the program checks whether resizing succeeded.
- 18
Why should a dynamically allocated array be freed after it is no longer needed?
Model answer
Freeing it returns its allocated memory to the computer and prevents a memory leak.
Explanation
A resized allocation also needs to be freed when the program is finished using it.
- 19
How can a pointer to an integer be used with square-bracket array notation?
Model answer
If it points to a sufficiently large contiguous block of integers, square-bracket notation accesses the integers at successive positions in that block.
Explanation
Dynamically allocated contiguous memory can be treated like an array for indexing.
- 20
When an integer occupies four bytes, how far apart are the addresses of successive integers in an allocated block?
Model answer
Successive integers are four bytes apart: index 1 is four bytes after index 0, and index 2 is eight bytes after index 0.
Explanation
Array indexing on a pointer advances by the size of the pointed-to type, rather than by just one byte.
- 21
How do arrays and pointers relate to contiguous memory in this context?
Model answer
An array is a contiguous chunk of memory, and a pointer can hold the address of a dynamically allocated contiguous chunk that can be accessed with array-style indexing.
Explanation
This relationship allows dynamically allocated memory to be used similarly to an array.
- 22
What is the main advantage of arrays for accessing elements?
Model answer
Arrays provide constant-time random access, allowing a program to jump directly to an indexed element.
Explanation
The computer can access a chosen array position without stepping through the earlier elements.
- 23
What search advantage does a sorted array provide?
Model answer
A sorted array supports binary search, which takes O(logn) time.
Explanation
Binary search repeatedly narrows the part of the array that could contain the target.
- 24
What is a node in the linked-list example, and what fields does each node contain?
Model answer
A node is a unit of a data structure that contains an integer value and a pointer to another node.
Explanation
The value stores the list's data, while the pointer links the node to the next one.
- 25
How does a linked list differ in memory layout from an array?
Model answer
A linked list's nodes can be located in separate, nonadjacent memory locations, while an array's elements are contiguous.
Explanation
Pointers connect linked-list nodes even when they are scattered in memory.
- 26
How does a linked list mark the end of the list?
Model answer
The final node's next pointer is set to
NULL.Explanation
The null pointer indicates that there is no next node.
- 27
How can an empty linked list be represented?
Model answer
It can be represented by a list pointer initialized to
NULL.Explanation
The pointer then indicates that there is no first node.
- 28
Why does creating a linked-list node require allocating space for the whole node rather than only for its integer?
Model answer
A node contains both its integer and its next pointer, so the allocation must be large enough for the entire structure.
Explanation
sizeofapplied to the node type can provide the required allocation size. - 29
What does the
->operator do when used with a pointer to a structure?Model answer
It accesses a field of the structure to which the pointer refers.
Explanation
For a node pointer, expressions such as
n->numberandn->nextaccess its fields. - 30
How is
n->numberrelated to dereferencingnand using the dot operator?Model answer
n->numberis shorthand for(*n).number: dereference the pointer, then access the structure'snumberfield.Explanation
The parentheses make the order of dereferencing and field access explicit.
- 31
How can a program traverse a linked list to find its end?
Model answer
Start at the first node and repeatedly follow each node's next pointer until the next pointer is
NULL.Explanation
A temporary pointer can move through the list without changing the list's head pointer.
- 32
Why can replacing a list's head pointer with a new node accidentally lose the rest of the list?
Model answer
If the new node does not first point to the old head, overwriting the head pointer leaves the old nodes without an accessible reference.
Explanation
Those nodes may then be orphaned, making their allocated memory impossible to reach through the list.
- 33
When inserting a new node at the beginning of a linked list, what pointer updates preserve the existing list?
Model answer
Set the new node's next pointer to the current head, then update the head pointer to the new node.
Explanation
The first update keeps the old list reachable before the head is changed.
- 34
When inserting a node into the middle of a linked list, which pointer should be updated first to avoid orphaning the remaining nodes?
Model answer
First make the new node point to the node that was originally next; then make the preceding node point to the new node.
Explanation
This order preserves the existing connection to the rest of the list.
- 35
Why can losing the only pointer to allocated nodes cause a memory leak?
Model answer
Without a pointer to the allocated memory, the program can no longer access or free it.
Explanation
A memory leak can result either from failing to free reachable memory or from losing track of allocated memory.
- 36
What is the benefit of a linked list when adding elements dynamically?
Model answer
It can grow by allocating additional nodes and linking them, without relocating all existing elements.
Explanation
Existing nodes can remain where they are in memory as the list grows.
- 37
Why does a linked list not support constant-time random access or ordinary binary search?
Model answer
To reach an element, the program must follow pointers from the list's beginning rather than jump directly to an index.
Explanation
Without direct access to the middle, a linked list cannot efficiently perform binary search.
- 38
What are the worst-case time complexities for searching a linked list and inserting into a sorted position?
Model answer
Both can take O(n) time because the program may need to traverse the list to find the target or insertion position.
Explanation
The work may involve examining nodes one at a time.
- 39
What extra memory cost does a linked list incur compared with storing only its values?
Model answer
Each node also stores a pointer, so the list uses memory for both its values and its links.
Explanation
The pointers provide the connections that let the nodes form a list.
- 40
What is a binary search tree?
Model answer
It is a tree in which each node has at most two children and values are arranged so that smaller values are to the left and larger values are to the right.
Explanation
The two possible child directions give the tree its binary structure and its search ordering.
- 41
Why is the ordering rule for a binary search tree described as recursive?
Model answer
The smaller-left and larger-right rule applies at every node and to the subtrees rooted at that node's children.
Explanation
Each subtree follows the same ordering rule as the entire tree.
- 42
What are the left and right pointers in a binary search tree node used for?
Model answer
They point to the node's left and right children, respectively.
Explanation
Using two pointers lets the structure branch in two directions.
- 43
What does the root pointer represent in a tree?
Model answer
It points to the topmost node, from which the rest of the tree can be reached.
Explanation
As with a list's head pointer, access to the structure begins at this pointer.
- 44
How does binary search tree ordering guide a search for a target value?
Model answer
Compare the target with the current node; go left if it is smaller, go right if it is larger, and report a match if it is equal.
Explanation
The ordering rule allows the search to discard the subtree on the wrong side.
- 45
What is the base case when searching a binary search tree, and what result should it produce?
Model answer
If the current tree pointer is
NULL, the target is absent from that subtree, so the search returns false.Explanation
An empty subtree contains no value to match.
- 46
Why is searching a binary search tree naturally suited to recursion?
Model answer
After comparing at a node, the search can apply the same procedure to the left or right subtree, which is itself a tree.
Explanation
The recursive calls follow the structure's repeated subtree organization.
- 47
What is the search time in a balanced binary search tree?
Model answer
It is O(logn) because each comparison can eliminate a substantial part of the remaining tree.
Explanation
A balanced tree has logarithmic height, so searching follows only a logarithmic number of levels.
- 48
How can repeatedly inserting values on the same side affect a binary search tree?
Model answer
The tree can become tall and unbalanced, reducing the efficiency of its searches.
Explanation
A tree's search efficiency depends on keeping its height from growing excessively.
- 49
How can a binary search tree be kept efficient as values are inserted?
Model answer
Balancing algorithms rearrange the links between nodes so the tree does not become excessively tall.
Explanation
The nodes can stay in memory while their pointers are updated to maintain a more balanced structure.
- 50
What is a hash table made of, and how does it organize its entries?
Model answer
A hash table combines an array of buckets with linked lists that can hold multiple entries in a bucket.
Explanation
The array provides bucket access, while linked lists help accommodate entries that map to the same bucket.
- 51
What is a hash function?
Model answer
A hash function takes an input and produces an output that can be used to choose a bucket in a hash table.
Explanation
For example, a function could use a name's first letter to select a bucket.
- 52
How could a hash table use a name's first letter to assign it to one of 26 buckets?
Model answer
It can map the first letter to an array index from 0 to 25 and store the name in that bucket.
Explanation
The letters provide a simple way to choose among 26 array positions.
- 53
What is a collision in a hash table?
Model answer
A collision occurs when an entry is assigned to a bucket that already contains another entry.
Explanation
Different names can produce the same bucket index under a hash function.
- 54
How can a hash table resolve collisions using linked lists?
Model answer
It can link entries assigned to the same bucket into a list stored at that bucket.
Explanation
The array still identifies the bucket, and the linked list holds its colliding entries.
- 55
Why can a first-letter hash function lead to slow searches in the worst case?
Model answer
If many names begin with the same letter, they accumulate in one bucket and may require a linear search through that bucket.
Explanation
In the worst case, all entries could collide in the same bucket, producing O(n) search time.
- 56
How might using the first two letters of a name as a hash key affect collisions?
Model answer
It may reduce collisions by distributing names across more buckets, but names with the same first two letters can still collide.
Explanation
Using two letters requires 26×26 possible buckets in the example.
- 57
What trade-off arises when a hash table uses more characters of each name to choose a bucket?
Model answer
Using more characters can reduce collisions but requires more buckets and therefore more memory.
Explanation
For example, three-letter combinations require many more buckets than first-letter indexing.
- 58
Why is a hash table often fast in practice even though its worst-case search time can be O(n)?
Model answer
A good hash function tends to distribute entries among buckets, so searches usually examine only a small number of entries.
Explanation
The theoretical worst case remains possible if many entries collide, but typical performance can be much faster.
- 59
What would an ideal hash function accomplish, and what is the related cost?
Model answer
An ideal function would place entries in unique buckets, enabling O(1) access, but achieving that can require more space and effort.
Explanation
The design balances the time saved against memory use and the difficulty of creating the function.
- 60
What is the central time-space trade-off illustrated by hash tables?
Model answer
Using more memory for buckets can reduce collisions and speed up access, while using fewer buckets saves space but can increase search time.
Explanation
The choice of bucket count and hash function affects both memory use and performance.
- 61
What is a trie, and how does it organize words?
Model answer
A trie is a tree whose nodes are arrays, organized by successive characters of words.
Explanation
Each character determines a path through the arrays.
- 62
How is a word such as “Harry” represented in a trie?
Model answer
Starting at the root, follow the array positions for its successive letters, allocating child arrays as needed.
Explanation
The path represents the word's character sequence.
- 63
How can a trie mark the end of a stored word?
Model answer
It can mark the node reached after the word's final character with a flag indicating that a word ends there.
Explanation
A Boolean flag can distinguish a complete word from a prefix of a longer word.
- 64
How can a trie share memory among words with common prefixes?
Model answer
Words with the same starting characters share the nodes along their common prefix and branch only when their characters differ.
Explanation
For example, words beginning with the same letter share the root's corresponding child.
- 65
What is the lookup time for a word in a trie in terms of the word's length?
Model answer
Lookup takes O(k) time, where k is the number of characters in the word.
Explanation
The search follows one node per character rather than examining all stored words.
- 66
Why can trie lookup be considered constant time relative to the number of stored words?
Model answer
The number of steps depends on the searched word's length, not on how many other words are stored; if word length is bounded, lookup is effectively O(1) with respect to the number of words.
Explanation
Adding more words does not require searching through those words when following a particular character path.
- 67
What is the main cost of using a trie to achieve fast lookup?
Model answer
A trie can consume substantial memory because each node contains an array of possible character branches.
Explanation
The fast character-by-character paths trade increased memory use for lookup speed.
- 68
What does FIFO mean for a queue?
Model answer
FIFO means “first in, first out”: elements are removed in the order they were added.
Explanation
A line for service is a real-world example of this ordering.
- 69
What are the two fundamental queue operations called?
Model answer
Enqueueadds an element to the queue, anddequeueremoves an element from it.Explanation
These operations correspond to getting into and leaving a line.
- 70
What does LIFO mean for a stack?
Model answer
LIFO means “last in, first out”: the most recently added element is removed first.
Explanation
A stack of trays is an example when people take the top tray.
- 71
What are the two fundamental stack operations called?
Model answer
Pushadds an element to the stack, andpopremoves the top element.Explanation
These operations implement the stack's last-in, first-out behavior.
- 72
How do queues and stacks differ in the order in which elements are removed?
Model answer
A queue removes the earliest-added element first, whereas a stack removes the latest-added element first.
Explanation
Queues are FIFO; stacks are LIFO.
- 73
What is a dictionary as an abstract data structure?
Model answer
A dictionary stores associations between keys and values, such as words and their related information.
Explanation
A hash table can be used as an implementation of a dictionary.
- 74
How can a dictionary support checking whether a word is present?
Model answer
It can store words as keys and look up a queried word to determine whether it exists.
Explanation
A spelling checker can use this operation to test whether a word is in its dictionary.
- 75
How can arrays and linked lists serve as building blocks for other data structures?
Model answer
They can be combined or organized to implement higher-level structures, such as using an array of linked lists for a hash table.
Explanation
Abstract structures describe useful operations, while arrays and lists can provide their underlying implementation.

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