fn_map
array mapping
C language
programming tutorial
code implementation

How to use fn_map to map each row in an array C to its coresponding one in the array B

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

When people say they want to map each row in array C to its corresponding row in array B, they usually mean one of two things: pair rows by index, or match rows by some key. If fn_map is your mapping helper, the important part is deciding what the callback receives and whether the relationship is positional or lookup-based.

The Simple Case: Row i in C Corresponds to Row i in B

If both arrays have the same number of rows and the rows correspond by position, the mapping logic is just a loop that calls a callback for each row pair.

c
1#include <stdio.h>
2
3typedef void (*row_mapper)(const int *crow, const int *brow, int cols);
4
5void fn_map(const int *C, const int *B, int rows, int cols, row_mapper mapper) {
6    for (int i = 0; i < rows; i++) {
7        const int *crow = C + i * cols;
8        const int *brow = B + i * cols;
9        mapper(crow, brow, cols);
10    }
11}
12
13void print_pair(const int *crow, const int *brow, int cols) {
14    for (int j = 0; j < cols; j++) {
15        printf("C=%d B=%d\n", crow[j], brow[j]);
16    }
17}

This pattern treats both arrays as flat memory and computes each row start with pointer arithmetic.

Example Usage

c
1int main(void) {
2    int C[2][3] = {
3        {1, 2, 3},
4        {4, 5, 6}
5    };
6
7    int B[2][3] = {
8        {10, 20, 30},
9        {40, 50, 60}
10    };
11
12    fn_map(&C[0][0], &B[0][0], 2, 3, print_pair);
13    return 0;
14}

The callback receives matching rows and can print them, combine them, compare them, or write the result into another array.

When the Mapping Is Not Positional

Sometimes the real problem is not index-based pairing. Instead, you may need to match each row in C to a row in B using an ID column or another key. In that case, a direct index loop is not enough.

A common approach is:

  1. Build a lookup structure from rows in B.
  2. For each row in C, compute the key.
  3. Find the matching row in B.
  4. Invoke the mapping callback with the matched pair.

That is still conceptually a map operation, but the expensive part moves into the lookup step rather than the callback itself.

Design the Callback Around the Real Goal

A useful fn_map design depends on what you want the callback to do. Common choices are:

  • transform row pairs into an output row
  • compare corresponding rows for equality or differences
  • accumulate statistics
  • print matched pairs for debugging

If the callback needs extra context, pass a user-data pointer as an additional parameter rather than relying on global state. That makes the function reusable and easier to test.

Common Pitfalls

  • Assuming rows correspond by index when the data actually needs key-based matching.
  • Forgetting that a two-dimensional C array is contiguous memory only when declared appropriately.
  • Hiding row length assumptions inside the callback instead of passing cols explicitly.
  • Using pointer arithmetic incorrectly and shifting to the wrong row boundary.
  • Naming the function fn_map without defining whether it transforms, compares, or accumulates.

Summary

  • First decide whether the row mapping is positional or key-based.
  • For positional mapping, a loop plus callback is enough.
  • In C, flat-memory row access is usually done with pointer arithmetic and explicit row and column counts.
  • Design the callback signature around the actual operation you need.
  • If the mapping depends on IDs, build a lookup rather than forcing index-based pairing.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms