Pointers to Multi-Dimension Arrays – Solution

The challenge for this month’s Exercise is to use pointers to work through a multi-dimensional array. As I mentioned in the exercise post, multi-dimensional arrays are all stored as a single-dimension where the compiler calculates offsets for rows and columns (and more) for you.

After populating the grid (using code from the challenge post), I present three approaches to use a pointer to navigate the multi-dimensional array.

The first way is to work through the array front to back. Remember, a multi-dimensional array is a single collection of data as it’s stored in memory.

The second way is to use a pointer to work through the dimensions, calculating offsets by rows and columns.

The third way also works through the array’s rows and columns, but with two pointers used to help calculate the offsets.

Here is my solution:

2026_05-Exercise.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 4
#define COL 3

int main()
{
    int values[ROW][COL];
    int x,y;
    int *a,*r,*c;

    /* seed the randomizer */
    srand( (unsigned)time(NULL) );

    /* populate and output the grid */
    for( y=0; y<ROW; y++ )
    {
        for( x=0; x<COL; x++ )
        {
            values[y][x] = rand() % 100;
            printf("[%d][%d] = %2d\t",y,x,values[y][x]);
        }
        putchar('\n');
    }

    /* initialize and use pointer *a */
    a = &values[0][0];
    for( x=0; x<ROW*COL; x++ )
        printf("%2d ",*(a+x) );
    putchar('\n');

    /* nested loop with pointer *a */
    a = &values[0][0];
    for( y=0; y<ROW; y++ )
    {
        for( x=0; x<COL; x++ )
        {
            printf("[%d][%d] = %2d\t",y,x,*(a+(y*COL+x)));
        }
        putchar('\n');
    }

    putchar('\n');        /* visual spacing */

    /* nested loop with pointers *r and *c */
    r = &values[0][0];
    for( y=0; y<ROW; y++ )
    {
        c = r+y*COL;
        for( x=0; x<COL; x++ )
        {
            printf("[%d][%d] = %2d\t",y,x,*(c+x));
        }
        putchar('\n');
    }

    return 0;
}

With each example, I use this statement to extract the base address of array values[][].

a = &values[0][0];

I haven’t figured out another way to obtain the base address of a multi-dimensional array, but the above approach works.

For my first solution, I use a for loop to output the array’s data one element at a time. The array’s size is calculated by multiplying defined constants ROW and COL. Variable x works through each element, with the pointer extracting them as *(a+x).

Because the data is stored in a grid, for my second solution I use a nested loop to emulate the array’s multi-dimensional storage. Variables y and x loop through the values from zero through ROW and COL, respectively. This approach offers the most complex solution to find each element’s offset within the array: *(a+(y*COL+x)).

The overhead for my second solution is a bit much, which is why I prefer my third solution: use two pointers to access a multi-dimensional array’s data. Again, I use a nested loop to navigate rows and columns. Pointer r is initialized at the array’s base. Pointer c represents the column offset in a two-dimensional array: c = r+y*COL. Once this calculation is made, obtaining the column values is accessed with the expression *(c+x), which is similar to my first solution.

Here is my solution’s output:

[0][0] = 87     [0][1] = 74     [0][2] = 25
[1][0] = 66     [1][1] = 30     [1][2] = 18
[2][0] = 50     [2][1] = 62     [2][2] = 51
[3][0] = 94     [3][1] = 26     [3][2] = 17
87 74 25 66 30 18 50 62 51 94 26 17
[0][0] = 87     [0][1] = 74     [0][2] = 25
[1][0] = 66     [1][1] = 30     [1][2] = 18
[2][0] = 50     [2][1] = 62     [2][2] = 51
[3][0] = 94     [3][1] = 26     [3][2] = 17

[0][0] = 87     [0][1] = 74     [0][2] = 25
[1][0] = 66     [1][1] = 30     [1][2] = 18
[2][0] = 50     [2][1] = 62     [2][2] = 51
[3][0] = 94     [3][1] = 26     [3][2] = 17

The first set of values output shows the data as it’s randomized and stored in the grid. The second set (the single line) is from my first approach. The next two sets are from my second and third methods for extracting the data.

The goal of this exercise is to show how you can use pointers to navigate through a multi-dimensional array. As the array is stored sequentially, I would defer to the first method. But for larger dimensions, I may opt for my last approach as it has the least amount of overhead and approaches the problem from a multi-dimensional perspective.

I hope that your solution(s) met with success!

Leave a Reply