Loading…
Loading…
Topic
Articles and problems tagged Pointers.
A complete application to finish the course: a parking garage whose lanes are linked-list queues, where a car leaving from the middle of a lane needs the cars in front of it moved out onto a stack and back. Combines queues, a stack, searching and careful pointer updates.
Over time a disk collects files with the same name in different folders. Walk every directory, insert each file name into a binary search tree, and report a duplicate the moment a name is found already in the tree. Includes a runnable version and a real directory walk with nftw().
How does a compiler get X = A / B + C * D - F * G / Q in the right order? Convert infix expressions to postfix with an operator stack, respecting priority and parentheses, then evaluate postfix with a number stack. Fixes the classic version's missing minus sign and empty-stack bug.
Two data structures every program relies on without seeing them: the circular queue that holds the keys you type before a program reads them, and the stack that remembers where each function call must return to. Simulate both, and see how recursion builds and unwinds stack frames.
Build a small generic database: a file that describes its own fields, fixed-size records you can jump to with fseek, and an index file that lists records in sorted order without moving them. Uses a linked list of tagged unions to sort values of any field type.
Speed up searching by splitting data into many short linked lists, reached through an array of pointers. Build a spelling dictionary with one sorted list per letter, then generate unique random numbers with a bucketed hash table, and learn what makes a good hash function.
See how the FAT file system, still used on USB sticks and memory cards, records where every file is stored: boot sector, FAT, directory and data area, clusters, and FAT chains that work like linked lists. Then decode 12-bit and 16-bit FAT entries correctly with pointers and bitwise operators.
Exercises with answers: animate the colours of video memory, make text rain down the screen, read and set the BIOS clock, decode the equipment word, predict outputs involving function pointers, far pointers and argv, and answer short questions on near, far and huge pointers.
On a DOS PC, a pointer to the right address could change the screen, switch on Caps Lock or read the memory size. Explore video memory, the keyboard status byte and the BIOS data area in a simulation, learn safe bit manipulation, and meet volatile pointers in embedded C.
Old 16-bit PCs reached 1 MB of memory by combining a segment and an offset, which gave C three kinds of pointer: near, far and huge. Learn how segment:offset addressing worked, why two far pointers to the same byte could compare unequal, and how normalization fixed it.
main receives its command-line arguments as argc and argv, an array of pointers to strings. And a function such as printf can take any number of arguments using stdarg.h. Learn both, including default promotions, va_end, and why a variadic function must be told how many arguments it got.
A function's code lives at an address too, and a pointer can hold it. Learn to declare and call function pointers, build arrays of them, pass them to functions as callbacks (as qsort does), return them, tame their syntax with typedef, and read even the most tangled C declarations.
Every file a C program opens is reached through a FILE *. Learn what that pointer points to (a structure holding a buffer and a position), how fopen, fgetc and fclose use it, and why the character read from a file must be stored in an int, never a char.
Seven exercises with full, runnable solutions: a stack as a circular linked list, a circular doubly linked list, preorder and postorder traversal without recursion, copying and comparing binary search trees with recursion, and inserting a node as a left child in a threaded binary tree.
Eight solved problems on linked lists and binary trees: concatenate two lists, erase a list, count, compare, copy and append with recursion, traverse a tree in inorder with your own stack instead of recursion, and mirror a tree. Each solution is complete, runnable and explained line by line.
A graph is a set of vertices joined by edges — the structure behind road maps, networks and circuits. Learn the vocabulary, store a graph as an adjacency matrix or as adjacency lists built from linked lists, and visit every reachable vertex with depth first search and breadth first search.
Traverse a binary tree in inorder with no recursion and no stack, by turning the NULL links of the leaves into threads to the inorder successor and predecessor. Build a threaded binary search tree with a head node, and insert and delete nodes while keeping every thread correct.
Remove a value from a binary search tree without breaking its ordering: the four cases (not found, leaf, one child, two children), the inorder successor trick, and a version built on a pointer to the father's link that also deletes the root correctly, where the classic version crashes.
Meet the tree, a non-linear data structure: binary tree terminology (root, leaf, level, depth, strictly binary, complete), the three traversals inorder, preorder and postorder, and a binary search tree in C that sorts numbers by inserting them and reading them back in order.
In a circular linked list the last node links back to the first, so the list has no NULL end. Build a queue on a circular list, learn how to traverse a list that never ends, keep the circle closed on every deletion, and see why a single rear pointer is enough.