The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate a cumulative sum for each position
C: Keep a running total and store each prefix result in a separate output array.
C✓ Tested
Filter an array in place with a write index
C: Only retained values advance the write position.
C✓ Tested
Find the smallest value in a nonempty array
C: Initialize from the first element instead of assuming a numeric sentinel.
C✓ Tested
Insert into an array with spare capacity
C: Move the trailing elements before inserting and keep the logical length separate from capacity.
C✓ Tested
Merge two sorted arrays
C: Advance the pointer belonging to the smaller next element and copy any remaining suffix.
C✓ Tested
Remove adjacent duplicates from a sorted array
C: Compact equal neighbors with a write index after sorting or otherwise establishing order.
C✓ Tested
Remove one array element and close the gap
C: memmove handles the overlap while the logical length shrinks by one.
C✓ Tested
Rotate a nonempty array left by one element
C: Save the first item before shifting the remaining elements.
C✓ Tested
Search a sorted array with bsearch
C: The search uses the same ordering comparator as the sorted input.
C✓ Tested
Sort an integer array with qsort
C: The comparator uses relational comparisons to avoid subtraction overflow.
C✓ Tested