I received a question via email regarding my recently-published book, Dan Gookin’s Guide to C Language Pointers (available at Amazon!). The question: How to use a pointer to navigate a multi-dimension array? While I do cover pointers to an array of strings (which is kinda what the reader wanted), I don’t specifically cover pointers and multi-dimensional arrays. Therefore, I created this month’s exercise to tackle the task.
Multi-dimensional arrays are useful for representing data in grids or matrixes. But the array notation used in C is only for your benefit; internally, the data is stored sequentially. The compiler does the math to ensure that the proper element is referenced no matter what dimensions are specified. Translating this data into pointers is a matter of approach coupled with various math thingies.
To start, I present the following code, which populates a two-dimensional array with random data:
2026_05_01-Lesson.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;
/* 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] = %d\t",y,x,values[y][x]);
}
putchar('\n');
}
return 0;
}
The program’s output changes with each run, but here’s a sample:
[0][0] = 36 [0][1] = 70 [0][2] = 65 [1][0] = 14 [1][1] = 27 [1][2] = 99 [2][0] = 13 [2][1] = 62 [2][2] = 4 [3][0] = 42 [3][1] = 54 [3][2] = 9
Your task for this month’s Exercise is to use one or more pointers to navigate through the matrix and output the data.
In my solution, I output the data three times in addition to the initial output above:
- I use a single point to output the values.
- I use a single pointer, but output the values in a grid format.
- I use two pointers to work through each dimension of the array.
The way you manage your solution is up to you, though I’ll present all three in mine.
As a hefty tip, be aware that obtaining the base address of a multi-dimension array isn’t as straightforward as obtaining the address of a single-dimension array. For a typical array, such as array[], you can assign pointer a to the address by using the following statement:
a = array;
This approach doesn’t work for multi-dimensional arrays. I don’t know if there is an established trick to make the assignment, so what I use is the following:
a = array[0][0];
Pointer a is assigned the address of the array’s first element. As arrays are stored sequentially in memory, this approach works. Feel free to use it in your code, unless you can think of something better.
I’ll post my solution in a week.