Loading…
Loading…
Topic
Articles and problems tagged Pointers.
Sort a linked list with selection sort and bubble sort in two ways: by exchanging the data inside the nodes, and by rearranging the links so the nodes themselves change order. See when each is better, and how a pointer to a link turns the four special cases of node swapping into two.
Store a polynomial such as 4x^5 + 2.5x^4 - 3x^2 + 6x + 1 as a linked list of terms, then add two polynomials in one pass and multiply them term by term, combining equal powers and removing terms that cancel. Every tricky line is explained.
Why linked lists beat arrays when data grows and shrinks, then merge two sorted linked lists into a third one in a single pass, keeping common values only once. Every pointer move is explained, including the empty-list case that the classic version crashes on.
Five exercises with full solutions: a recursive determinant using calloc, reading and changing file attributes in a raw directory entry, a sorted directory listing with a linked list, and decoding a boot sector and partition table byte by byte.
Five exercises on structures and pointers: arrays of pointers to structures, a list of malloc'd nodes, 2-D char arrays in a structure, nested structures through a pointer, and calling a function before declaring it. Predict each output, then find the hidden bug.
Harder puzzles on structures that point to structures: ++ and -- inside ->, arrays of pointers to structures, swapping through structure pointers, malloc'd nodes, a two-node doubly linked list, and a circular list that never ends. Predict each output, then follow the step-by-step explanation.
Worked puzzles on structures: the dot versus arrow rule, the addresses and padding of structure members, arrays of structures, structures holding char arrays and char pointers, nested structures, and arrays of structures that point to each other. Predict each output, then read the explanation.
In a doubly linked list each node points to both the next and the previous node, so you can walk either way and delete a node without searching for its predecessor. Build one with append, insert, delete and two-way display, with every link change drawn.
Build a stack (last in, first out), a queue (first in, first out) and a deque (both ends) on top of linked lists, so they can grow without a fixed size. Learn push, pop, enqueue and dequeue, why front and rear must stay in sync, and how to report an empty structure safely.
Keep a linked list in ascending order as nodes are added, by finding the right gap and splicing the new node in. Then reverse a whole list in place with three pointers. Includes a short-circuit bug that crashes the classic version, and a neat pointer-to-pointer technique.
A linked list stores data in nodes scattered through memory, chained together by pointers. Build one step by step: append, add at the beginning, insert after a node, display, count, delete and free, with diagrams of every link change and the reason functions take a struct node **.
Point at a structure, reach its members with ->, pass structures by address, walk arrays of structures with pointers and create structures with malloc. Then see how member offsets work, and how offsetof finds a whole structure from a pointer to one member.
Real-world data rarely comes as a single int or float. Learn how a C structure groups dissimilar data into one unit: declaring structures, the dot operator, how members sit in memory (including padding), arrays of structures, nesting, copying and passing structures to functions.
Write string functions yourself: find a character, find a substring, sort names, compress repeated spaces and encode a message. Each exercise has a hint and a full, runnable solution.
Short questions on strcat with literals, defining your own strcpy, and processing strings, followed by output puzzles that combine pointers, strcmp, strcat and 3-D char arrays.
Puzzles with pointers walking through strings: printing backwards, two pointers meeting in the middle, one-line string copy, three levels of pointers, and arrays of strings.
Worked puzzles on strings: the '\0' terminator, adding numbers to string literals, comparing strings with ==, sizeof on arrays of pointers and more. Predict the output, then check the explanation.
Store a list of strings as a 2-D char array or as an array of pointers. Compare memory use on 64-bit systems, swapping strings, and reading strings in with malloc, with clear diagrams.
char arrays vs pointers to string literals: what can be assigned, what can be changed, and why modifying a literal crashes. Then master const, including const char *, char *const and returning const.
Learn strlen, strcpy, strcat and strcmp from <string.h>, then write each one yourself with pointers. Includes buffer overflow pitfalls, why strcmp only guarantees a sign, and safer snprintf.