I Need Help... [Multi-dimention Arrays & Pointers]

So, where to begin..?
I'll start with the Problem. I've been trying to get my program going but it doesn't work. I think that the Problem results from my non-understanding of the workings of pointers and parameters, but as I'm a basic noobie at this it isn't certain.
My code produces the following:
cannot convert `int (*)[7]' to `int*' for argument `3' to `int MapDisplay(int*, int, int*, int)'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
int MapDisplay (int *temparray1,int length, int *MapArray, int NumCols)
{
    int **UnitInfoArrayCpy;
    UnitInfoArrayCpy = &temparray1;

    for (int m; m< length; m++)
    {std::cout<<UnitInfoArrayCpy[m]<<" O \n";};
    
    int temp1;
    int temp2;
    int temp3;
	
    for (int i = 0; i <= length; i+ 5)
    {
        if (UnitInfoArrayCpy[i+2] != 0)
        {  
        temp1 = *UnitInfoArrayCpy[i];
        temp2 = *UnitInfoArrayCpy[i+1];
        temp3 = MapArray[temp1 * NumCols + temp2];
        temp3 = temp3 + (*UnitInfoArrayCpy[i+3] * 10);
        MapArray[temp1 * NumCols + temp2] = temp3;
        };
    };
    
 
	std::cout<<"Map:"<<"\n \n";
	for (int nRow = 0; nRow < nNumRows; nRow++)
	{
		for (int nCol = 0; nCol < nNumCols; nCol++)
		{
                //This part would contain a very long cout, so I'll omit it to save space
		};
		std::cout <<"\n" << "\n";
	};
}


int main ()
{   

    const int nNumRows = 5;
    const int nNumCols = 7;

    int MapArray[nNumRows][nNumCols] =
    {
    { 3, 3, 1, 2, 5, 5, 1, },
    { 3, 6, 6, 2, 5, 1, 1, }, 
    { 2, 2, 2, 2, 2, 2, 2, },
    { 4, 1, 1, 1, 3, 3, 3, },
    { 4, 4, 1, 1, 3, 3, 4, }
    };


    int UnitInfoArray[] = {1,1,10,1,1,1,3,10,2,2};
    int lengthUnitInfo = sizeof(UnitInfoArray)/sizeof(int);


    MapDisplay (UnitInfoArray, lengthUnitInfo, MapArray, nNumCols);
        
 
}


Any help, tip, advice, correction and/or constructive criticism would be greatly appreciated.
Thx in advance.
Last edited on
closed account (Dy7SLyTq)
try just MapArray instead of *MapArray on line 44
@DTSCode Thanks for the response, but it doesn't work...
To be honest I mistyped and the error occurs when I correct it as you suggested. If I try to compile it with an asterisk the program freezes and crashes. (I'll edit the 1st post in this fashion, sry for the confusion)
There's not enough code to reproduce the problem, but based on the error message, you're passing a pointer to an array of seven ints (int (*)[7]) to MapDisplay as the third argument (where it expects a pointer to int).

Based on the usage of that pointer MapArray[temp1 * NumCols + temp2]; inside the function, I assume that MapArray is a 2D array (whose second dimension is 7), and you're reinterpreting it as a 1D array. In that case, the proper call would be:

MapDisplay (UnitInfoArray, lengthUnitInfo, &MapArray[0][0], nNumCols);
@Cubbi Thanks a lot for your help.
I've tried to add the missing pieces of the program needed (parts are included from headers).

Taking your advice to heart I tried to alter the call to MapDisplay (UnitInfoArray, lengthUnitInfo, &MapArray[0][0], nNumCols);
Sadly the whole program freezes and outputs nothing... I had a similar problem before which I got rid of by fiddling with the pointers yet the 1st error ensued.
Could the freezing be a result of a problem with pointers? Or is something else at fault?
There are other issues. Here's the first one I see inside the function itself:

1
2
    int **UnitInfoArrayCpy;   
    UnitInfoArrayCpy = &temparray1;


This made UnitInfoArrayCpy a pointer to a pointer to the first element of main()'s UnitInfoArray.

1
2
3
    for (int i = 0; i <= length; i+ 5)
    {
        if (UnitInfoArrayCpy[i+2] != 0)

Besides the silly "i+5", which does nothing at all and loops forever, these is an error for any i other than -2: UnitInfoArrayCpy is not a pointer to an element of any array. The only valid access is UnitInfoArrayCpy[0], which is the same as *UnitInfoArrayCpy

Incidentally, Is there a reason you're using arrays and not vectors?
Ok, I see what you mean... at this point I can safely say that my code is extremely way to well... bad to save.
@Cubbi the reason i didn't use vectors is that I had no clue they existed^^. I'll look into them and start redo this part of my program.
Thank you for all your help. I appreciate it a lot. I'll get back to you if I need some more help^^.
Topic archived. No new replies allowed.