# Thread: Use pointer for multidimension array

1. ## Use pointer for multidimension array

I have learnt how to use pointer for multidimension array. this is my example:
Code:
```int trace [3][4];
int (*ptr)[4] =trace;```
After that,I don't know how to use pointer ptr to process elements of trace array.

thanks for tons

2. >int(*ptr)[4] =trace;
This... AFAIK... is useless.
You can already use trace as a pointer.
"trace[i][j]" gives the required element, so does " *(trace+i*4+j) "

3. Yes, I know this way too. there three ways same same like that to touch trace[i][j]:
Code:
```1) *(*(trace +i)+j)
2)*(trace[i]+j)
3)*(trace+i)[j]```
these ways easy to understand why. but the above example, in my book say that, but they don't say how to use this way
I think will exist a method to use this

@: manasij: I see that you like to use slang to talk today, I have learnt word: asap: as soon as possible. and now, new word:afaik

4. afaik = as far as I know. You can find most of these by googling "acronym afaik".

I agree with manasij7479 that using a pointer here is generally more awkward than using the index, but there are times when a pointer is easier or tidier. So if you need an example of how to walk a matrix that way:

Code:
```#include <iostream>

using namespace std;

int main(void) {
int ray[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} },
(*p1)[3] = ray, *p2 = p1[0];

while (p1 - ray < 3) {
if (p2 - *p1 == 3) {
cout << endl;
p1++;
p2 = p1[0];
} else {
cout << *p2 << ' ';
p2++;
}
}

return 0;
}```
It is very important when doing this that the pointers are correctly typed!

Some things to notice: p1 is effectively the same type as "ray" (technically, ray is an array while p1 is just a pointer, but ray implicitly converts to a pointer in context). However, p2 is not the same type as p1, and so when doing arithmetic with addresses, you must dereference (via *) p1 to make them equivalent, as on line 10 (just as you must dereference p2 on line 15 to get a value).

5. Oh, thanks very much to help me solve my problem. I have understand your code. It likes this idea:
Code:
```int trace[4];
int ptr*=trace;
// So: (ptr+1)=trace[1]; (ptr+2)=trace[2];```
And, I have an other question bulding from above problem:
I have this code:
Code:
```   int trace[3][4];
int (*ptr)[3]=trace;```
the complier will notice this error:cannot convert 'int (*)[4]' to 'int (*)[3]' in initialization.
I know C++ associate demension of array with type of variable, and int(*)[4] and int(*)[3] cannot convert together. But, why this line:
Code:
` int(*ptr)[3]=trace`
, C++will know type of trace is int(*)[4]. Can you teach me more, please.

thanks

6. An array is converted to a pointer to its first element. So, when you have an array of 3 arrays of 4 ints, it is converted to a pointer to an array of 4 ints, i.e., int (*)[4]. This is different from, and incompatible with, a pointer to an array of 3 ints.