Lesson 6 of 6
Mini Project Part 5 - Pointer-Based Array Utilities Library
Build a reusable array utilities library using pointer arithmetic: `sum`, `max`, `min`, `find`, `reverse`, `fill`, and `count_if` - all taking `int*` + length, using proper `const` annotation for read-only operations, and a predicate callback (preview of function pointers). This project demonstrates that pointer arithmetic and array indexing are equivalent and both appropriate in different contexts.
Array Utilities Library - Pointer-Based Implementation
/* array_utils.h */
#ifndef ARRAY_UTILS_H
#define ARRAY_UTILS_H
#include <stddef.h>
typedef int (*Predicate)(int);
int arr_sum (const int *arr, size_t n);
int arr_max (const int *arr, size_t n);
int arr_min (const int *arr, size_t n);
int *arr_find (const int *arr, size_t n, int target);
void arr_reverse(int *arr, size_t n);
void arr_fill (int *arr, size_t n, int value);
size_t arr_count_if(const int *arr, size_t n, Predicate pred);
void arr_print (const int *arr, size_t n, const char *label);
#endif
/* array_utils.c */
#include <stdio.h>
#include "array_utils.h"
int arr_sum(const int *arr, size_t n) {
int s = 0;
for (size_t i = 0; i < n; i++) s += arr[i];
return s;
}
int arr_max(const int *arr, size_t n) {
if (n == 0) return 0;
int m = arr[0];
for (size_t i = 1; i < n; i++) if (arr[i] > m) m = arr[i];
return m;
}
int arr_min(const int *arr, size_t n) {
if (n == 0) return 0;
int m = arr[0];
for (size_t i = 1; i < n; i++) if (arr[i] < m) m = arr[i];
return m;
}
/* Returns pointer to first match, or NULL if not found */
int *arr_find(const int *arr, size_t n, int target) {
for (size_t i = 0; i < n; i++)
if (arr[i] == target) return (int *)&arr[i];
return NULL;
}
void arr_reverse(int *arr, size_t n) {
int *lo = arr, *hi = arr + n - 1;
while (lo < hi) {
int tmp = *lo; *lo = *hi; *hi = tmp;
lo++; hi--;
}
}
void arr_fill(int *arr, size_t n, int value) {
for (size_t i = 0; i < n; i++) arr[i] = value;
}
size_t arr_count_if(const int *arr, size_t n, Predicate pred) {
size_t count = 0;
for (size_t i = 0; i < n; i++) if (pred(arr[i])) count++;
return count;
}
void arr_print(const int *arr, size_t n, const char *label) {
printf("%-10s: [", label);
for (size_t i = 0; i < n; i++) printf("%d%s", arr[i], i<n-1?", ":"");
printf("]
");
}
/* main.c */
/*
#include <stdio.h>
#include "array_utils.h"
static int is_even(int n) { return n % 2 == 0; }
static int is_positive(int n) { return n > 0; }
int main(void) {
int arr[] = {3, -1, 4, 1, -5, 9, 2, -6, 5, 3};
size_t n = sizeof(arr)/sizeof(arr[0]);
arr_print(arr, n, "original");
printf("sum = %d
", arr_sum(arr, n));
printf("max = %d
", arr_max(arr, n));
printf("min = %d
", arr_min(arr, n));
printf("evens = %zu
", arr_count_if(arr, n, is_even));
printf("pos = %zu
", arr_count_if(arr, n, is_positive));
int *found = arr_find(arr, n, 9);
if (found) printf("found 9 at index %td
", found - arr);
arr_reverse(arr, n);
arr_print(arr, n, "reversed");
return 0;
}
*/
/* Build: gcc -Wall -Wextra -std=c99 array_utils.c main.c -o arr_demo */Common Mistakes
- Returning a pointer to a local array - `int *f(void) { int arr[5]; return arr; }` - `arr` is on the stack and destroyed when `f` returns. The returned pointer is dangling. Return heap-allocated arrays or pass in a caller's array.
- casting away const in `arr_find` - the function takes `const int*` but returns `int*` via cast. This is safe only because we know the original data was writable. In truly const contexts this would be a correctness error.
- Size vs index type mismatch - loop indices for size_t arrays should be `size_t`, not `int`. `for (int i = 0; i < n; i++)` when `n` is `size_t` generates `-Wsign-compare` warnings. Use `for (size_t i = 0; i < n; i++)`.
Tip
Tip
Practice Mini Project Part 5 PointerBased Array Utilities Library in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Generic pointer utilities operate on any data type using byte offsets and element sizes
Practice Task
Note
Practice Task - (1) Write a working example of Mini Project Part 5 PointerBased Array Utilities Library from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Mini Project Part 5 PointerBased Array Utilities Library is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- Build a reusable array utilities library using pointer arithmetic: `sum`, `max`, `min`, `find`, `reverse`, `fill`, and `count_if` - all taking `int*` + length, using proper `const` annotation for read-only operations, and a predicate callback (preview of function pointers).
- Returning a pointer to a local array - `int *f(void) { int arr[5]; return arr; }` - `arr` is on the stack and destroyed when `f` returns. The returned pointer is dangling. Return heap-allocated arrays or pass in a caller's array.
- casting away const in `arr_find` - the function takes `const int*` but returns `int*` via cast. This is safe only because we know the original data was writable. In truly const contexts this would be a correctness error.
- Size vs index type mismatch - loop indices for size_t arrays should be `size_t`, not `int`. `for (int i = 0; i < n; i++)` when `n` is `size_t` generates `-Wsign-compare` warnings. Use `for (size_t i = 0; i < n; i++)`.
Finished this lesson?