Matrix Navigation via Pointer

Hey there,
I want to navigate a 2D Matrix (for example: int iMatrix[10][10]), with a pointer.

I'm not sure how to traverse this matrix in both demensions with a single pointer.

I want to know because I'm going to pass a pointer of this matrix into a function and do work with it.
You need to show some code to illustrate your problem/question.

1
2
int iMatrix[10][10] = { 0 }; // here is the matrix
int* ip = NULL; // here is the pointer 

I want to use the pointer, to iterate over this entire matrix or use an iterator.

How do I navigate this matrix with the pointer?

Or..

Do you have a better suggestion to iterate over this matrix?

I need to iterate over this array within a function. Hence, the pointer.

Here's an example.
I have the integer pointer "ip" inside a function, pointing to the the first element in the matrix; "iMatrix[0][0]".

So if I want to access the data at the index iMatrix[5][5] in a function, how do I do that with my pointer "ip"?
I found this link that I think is something close to what I'm looking for.

Method 7:
http://nadeausoftware.com/articles/2012/06/c_c_tip_how_loop_through_multi_dimensional_arrays_quickly#Method7Nestedloopswithlineararrayandsingleincrementingpointer

I'll have to take a closer look at it, after some sleep.
I'll share the code block I came up with, using segments from the link above; for anyone in future that is looking for a similar solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Working Variables
//---------------------------------------------------------
const int matrixWidth(5);  // etc.
const int matrixHeight(3); // etc.
int*      iMatrix[matrixWidth * matrixHeight] = {0};
int*      ip(iMatrix);
int       index(0);

for(int i = 0; i < (matrixWidth - 1); i++)
   for(int j = 0; j < (matrixHeight - 1); j++)
   {
       // Get the index
       index = (i * matrixHeight) + j;
       
       // To access matrix element (just an example)
       ip[index]; 

       // Do work

       // [ ... ]
    }

Now you'll notice that this doesn't look like a 2D array.
I've created the matrix like "iMatrix[]" and not "iMatrix[][]".
Creating the "matrix" in-line, maximizes the efficiency of the data access and navigation; at the cost of a tiny bit more code.
Not to mention, you can navigate it with a single pointer.

You create enough elements in the in-line array to house your 2D array (i.e. like so: matrixWidth * matrixHeight).

To get the index of an element of the in-line matrix you use this algorithm: "(x-coord * matrixHeight) + y-coord".
To navigate on the x-axis you add/subtract the "x-distance * matrixHeight".
To navigate on the y-axis you add/subtract the "y-distance".

To understand how this works, (if you don't intuitively understand) visualise the array like this:

matrixWidth = 5;
matrixHeight = 3;

[0, 1, 2 | 3, 4, 5 | 6, 7, 8 | 9, 10, 11 | 12, 13, 14]

0, 3, 6, 9, 12
1, 4, 7, 10, 13,
2, 5, 8, 11, 14

I hope this helps someone in the future! ^_^

NOTE: Remember that the index-0 element of an array is the first element in the array. This is important because the X-MAXIMUM and Y-MAXIMUM is the demensions of the matrix -1.
"matrixWidth - 1", "matrixHeight - 1".
Last edited on
Topic archived. No new replies allowed.