1. Home
  2. Explore
  3. Computer Science
  4. C Data Structures and Pointers

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

C Data Structures and Pointers

  • 68Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Memory and Pointers
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What is a pointer in C?

Tap to flipClick to flipspace

A variable that stores the address of a location in memory.

Or swipe the card

68 cards

  1. What is a pointer in C?

    A variable that stores the address of a location in memory.

  2. What does int *x declare?

    A pointer named x that can store the address of an integer.

  3. What does malloc do, and what does it return?

    It allocates a requested number of bytes and returns the address of the allocated memory.

  4. How can you request enough memory for one integer without hard-coding its byte size?

    Use malloc(sizeof(int)).

  5. What does the dereference operator * do when applied to a pointer?

    It accesses the value stored at the memory address the pointer contains.

  6. 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.

  7. If y = x, what happens to the pointers and their pointee?

    y receives the same address as x; both pointers refer to the same memory, which is not copied.

  8. What happens if *x = 42 is followed by *y = 13 when x and y point to the same location?

    The shared value becomes 13; the assignment overwrites 42.

  9. What should a program check after calling malloc?

    Whether the returned pointer is NULL, which indicates allocation failed.

  10. What does free(p) do?

    It releases the dynamically allocated memory that p points to.

  11. How are NULL and the character \0 different in C?

    NULL is a null pointer value; \0 is the null character. Both are represented by zero underneath.

  12. 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.

  13. 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.

  14. 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.

  15. What is the worst-case time complexity of growing an array by copying its existing elements?

    O(n)O(n)O(n).

  16. What is the time complexity of accessing an array element by index?

    O(1)O(1)O(1).

  17. What search method can search a sorted array in O(log⁡n)O(\log n)O(logn) time?

    Binary search.

  18. How can malloc allocate space for three integers?

    Use malloc(3 * sizeof(int)) and store the returned address in an integer pointer.

  19. 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.

  20. 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.

  21. What does realloc do?

    It resizes an existing allocated block, growing or shrinking it, and handles preserving or moving its contents.

  22. Why should the result of realloc often be stored in a temporary pointer?

    If reallocation fails and returns NULL, the original pointer is still available to the program.

  23. Which header file provides malloc, free, and realloc in C?

    stdlib.h.

  24. What is a node in a linked list?

    A unit of memory that stores a value and a pointer linking it to another node.

  25. What is a linked list?

    A sequence of nodes connected by pointers, which may be stored in noncontiguous memory.

  26. What fields might a node in a linked list of integers contain?

    An integer value and a pointer to the next node.

  27. Why must a self-referential C structure use struct node *next rather than a node *next alias inside its own definition?

    The node typedef alias is not available until the structure definition is complete.

  28. How is an empty linked list represented?

    By a list pointer set to NULL.

  29. 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.

  30. Why is a newly allocated node's next field often set to NULL?

    It marks that there is currently no next node, such as when the new node is at the end of the list.

  31. What does the C expression n->number mean?

    Access the number field of the node pointed to by n.

  32. How can a program traverse a linked list to find its end?

    Start at the head and repeatedly follow next pointers until the current node's next is NULL.

  33. When inserting a node at the beginning of a linked list, what pointer updates preserve the existing list?

    Set the new node's next to the old head, then set the head to the new node.

  34. When inserting a node between two linked-list nodes, which link should be set first?

    Set the new node's next to the node that should follow it, then link the preceding node to the new node.

  35. What is the worst-case search time for a linked list?

    O(n)O(n)O(n).

  36. 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.

  37. What is the worst-case time to insert into a sorted linked list when the correct position must first be found?

    O(n)O(n)O(n).

  38. 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.

  39. What is the main cost of linked lists compared with arrays?

    They use extra memory for pointers and lose constant-time indexed access.

  40. What does a linked list's head pointer need to identify the entire list?

    The address of the first node.

  41. What is a binary tree?

    A tree in which each node has at most two children.

  42. 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.

  43. What pointers does a binary search tree node typically need?

    A left pointer and a right pointer, plus its stored value.

  44. What is the root of a tree?

    The topmost starting node from which the rest of the tree can be reached.

  45. 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.

  46. 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.

  47. What is the search time in a balanced binary search tree?

    O(log⁡n)O(\log n)O(logn).

  48. Why can an unbalanced binary search tree have poor search performance?

    It can become very tall, making searches take up to O(n)O(n)O(n) time.

  49. 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.

  50. What is a hash function?

    A function that takes an input and produces an output used to determine where that input is stored.

  51. 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.

  52. What is a collision in a hash table?

    When multiple inputs map to the same bucket.

  53. How can a hash table resolve collisions using linked lists?

    Store colliding values in a linked list attached to their shared bucket.

  54. Why can a hash table have O(n)O(n)O(n) worst-case search time?

    All values could collide into one bucket, creating a chain that must be searched linearly.

  55. 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.

  56. What trade-off does a hash table illustrate?

    Using more memory or a more sophisticated hash function can improve lookup speed and reduce collisions.

  57. What is a trie?

    A tree-like structure whose nodes represent arrays of possible next characters.

  58. 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.

  59. 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.

  60. What is the main cost of using a trie?

    High memory usage, because each node may contain an array of possible character branches.

  61. What does FIFO mean for a queue?

    First in, first out: the earliest item added is the first item removed.

  62. What are the two basic queue operations?

    Enqueue adds an item; dequeue removes an item.

  63. What does LIFO mean for a stack?

    Last in, first out: the most recently added item is the first removed.

  64. What are the two basic stack operations?

    Push adds an item to the top; pop removes the top item.

  65. 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).

  66. 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.

  67. 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.

  68. 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.

Card 1 of 68
Card 1 of 68

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent