Passing 2D array into function using pointer

I've written the following program. I am trying to pass the 2D array into function using pointer and change the value and reassign it into the array.

But it is giving error..how it works? How do I fix it?

The error compiler shows: 10|error: invalid types ‘int[int]’ for array subscript|

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
#include <bits/stdc++.h>

using namespace std;

void firstchange(int *a, int n)
{
    for(int i=0; i<n; i++)
        for(int j=i; j<n; j++)
        {
            *(a[i][j])=*(a[i][j]) *7;
        }
}

void secondchange(int *b, int n)
{
    for(int i=0; i<n; i++)
        for(int j=i; j<n; j++)
        {
            b[i][j]=b[i][j] +5;
        }
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);

    int n=3;

    int FirstArray[n][n] = {1,2,3,4,5,6,7,8,9};
    int SecondArray[n][n] = {1,2,3,4,5,6,7,8,9};

    firstchange(FirstArray,n);
    secondchange(SecondArray,n);

    for(int i=0; i<n; i++)
    {
        for(int j=i; j<n; j++)
        {
            cout<<FirstArray[i][j]<<" ";
        }
        cout<<endl;
    }

    for(int i=0; i<n; i++)
    {
        for(int j=i; j<n; j++)
        {
            cout<<SecondArray[i][j]<<" ";
        }
        cout<<endl;
    }

    return 0;
}
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
#include <iostream>

using namespace std;

const int n = 3;

void firstchange(int a[n][n], int n)
{
    for(int i = 0; i<n; i++)
        for(int j = 0; j<n; j++)
        {
			int temp = (a[i][j]) * 7;
            a[i][j] = temp;
        }
}

void secondchange(int b[n][n], int n)
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
			int temp = (b[i][j]) +5;
            b[i][j]= temp;
        }
}

int main()
{
    
    int FirstArray[n][n] = {1,2,3,4,5,6,7,8,9};
    int SecondArray[n][n] = {1,2,3,4,5,6,7,8,9};

     firstchange(FirstArray,n);
     secondchange(SecondArray,n);

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cout<<FirstArray[i][j]<<" ";
        }
        cout<<endl;
    }

	std::cout << "2nd " << std::endl;

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            cout<<SecondArray[i][j]<<" ";
        }
        cout<<endl;
    }

	int x; 
	cin >> x;
    return 0;
}
Last edited on
You didn't passed the array using pointer. I need to pass the array into the functions using pointer.
closed account (SECMoG1T)
Hi, here you go

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
#include <bits/stdc++.h>

using namespace std;

template<std::size_t N,std::size_t M>
void firstchange(int (*a)[N][M])
{
    for(unsigned int i=0; i<N; i++)
        for(unsigned int j=i; j<M; j++)
        {
            (*a)[i][j]=(*a)[i][j]*7;
        }
}

template<std::size_t N,std::size_t M>
 void secondchange(int (*b)[N][M])
 {
      for(unsigned int i=0; i<N; i++)
        for(unsigned int j=i; j<M; j++)
        {
            (*b)[i][j]=(*b)[i][j]+5;
        }
}

 int main()
 {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
      const int n=3;///static array size should be constant

      int FirstArray[n][n] = {1,2,3,4,5,6,7,8,9};
      int SecondArray[n][n] = {1,2,3,4,5,6,7,8,9};

      int (*frstptr)[n][n]=&FirstArray;
      int (*scndptr)[n][n]=&SecondArray;

      firstchange(frstptr);
      secondchange(scndptr);

      for(unsigned int i=0; i<n; i++)
        {
            for(unsigned int j=i; j<n; j++)
            {
                 cout<<FirstArray[i][j]<<" ";
            }

            cout<<endl;
        }

        for(unsigned int i=0; i<n; i++)
        {
             for(unsigned int j=i; j<n; j++)
             {
                 cout<<SecondArray[i][j]<<" ";
             }

             cout<<endl;
        }

         return 0;
    }


indeed that's how you pass an array parameter to *anything* using a pointer .
Last edited on
Topic archived. No new replies allowed.