arrays and pointers

i use this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void arrive(int **Mem_in)
{
....
}
int main()
{

int Mem_in[100][5];
for(int i=0;i<100;i++)
{
	for(int j=0;j<5;j++)
	Mem_in[i][j]=0;
}
arrive(Mem_in)
}


but i get this error..

'arrive' : cannot convert parameter 1 from 'int [100][5]' to 'int **'

any idea what am i doing wrong??
Well, arrays are not exactly the same thing as pointers. You can do something like the above when you have a one-dimensional array, but not here. A decent solution here would be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

template <int Dim1, int Dim2>
void arrive(int Mem_in[Dim1][Dim2])
{
    cout << "cool!" << endl;
}

int main()
{
    int Mem_in[100][5];

    for(int i=0;i<100;i++)
        for(int j=0;j<5;j++)
            Mem_in[i][j]=0;

    arrive<100,5>(Mem_in);

    cin.get();
    return 0;
}

Useful links:

http://cplusplus.com/doc/tutorial/arrays/#multidimensional_arrays
http://cplusplus.com/doc/tutorial/templates/ (see function templates and non-type parameters for templates)
so i have to define the dimensions of the array..
and if i dont know them??
or i have to know them?
The dimensions have to be known at compile time.
If you don't know the dimensions, use vectors.

Well... There is another option...

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
#include <iostream>
using namespace std;

void print(int * array, int dim1, int dim2)
{
    for (int i=0; i<dim1; i++)
    {
        for (int j=0; j<dim2; j++)
            cout << array[i*dim2+j] << ' ';
        cout << endl;
    }
}

int main()
{
    int d1=2; int d2=3;

    //instead of a two-dimensional (d1) x (d2) array
    //use a one-dimensional (d1*d2) array
    int * Mem_in=new int[d1*d2];

    for(int i=0;i<d1;i++)
        for(int j=0;j<d2;j++)
            Mem_in[i*d2+j]=i+j;

    print(Mem_in,d1,d2);

    delete[] Mem_in;

    cin.get();
    return 0;
}

This way, the dimensions are not required to be known at compile time
and you get something slightly faster than nested vectors, but it's kind of messier.

Useful link -> http://cplusplus.com/doc/tutorial/dynamic/
Last edited on
ok thank you very much i will do it like that because the vectors are very slow against tables..
Due to the pattern emerging wouldn't it be faster to make something like
1
2
3
4
5
6
7
for(int i=0; i< d1; ++i)      // MAKES THE FIRST LINE BE 1 THROUGH WHATEVER
    Mem_in[i]=i;
for(int i=1, j=0; i*j<d1*d2;){
    Mem_in[i*d2+j]=Mem_in[(i-1)*d2+j]+1;     // MAKE EACH NEXT LINE BE THE ONE ABOVE+1
    ++i;
    ++j;        //  DOUBT  for(  ...  ;++i,++j) IS LEGAL
}
??

EDIT:
OK, so I did a quick test but, is seems something's off...
Output for d1=10,d2=9 is:
012345678
100000000
010000000
001000000
000100000...

Seems code written while extremely sleepy and with a quasi-headache isn't up to par; who knew!?
Last edited on
Hi

You can eliminate the size of only ONE dimension.
There a possible way to do it is the following


1.
void arrive(int *Mem_in[100])
{
....
}
int main()
{

int Mem_in[100][5];
for(int i=0;i<100;i++)
{
for(int j=0;j<5;j++)
Mem_in[i][j]=0;
}
arrive(Mem_in)
}

another similar way is to do :
2.
void arrive(int Mem_in[100][])
{
....
}
int main()
{

int Mem_in[100][5];
for(int i=0;i<100;i++)
{
for(int j=0;j<5;j++)
Mem_in[i][j]=0;
}
arrive(Mem_in)
}


and another way (better ) is to send the unkbown dimension as a parameter as follows :
3.
void arrive(int *Mem_in[100], int dimension2)
{
....
}
int main()
{

int Mem_in[100][5];
for(int i=0;i<100;i++)
{
for(int j=0;j<5;j++)
Mem_in[i][j]=0;
}
arrive(Mem_in, 5)
}

Hope this helps
If anything is wrong , Please let me know .
Topic archived. No new replies allowed.