Why is it giving me an error in the function call? Am I calling the array incorrectly?

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

int fillArrayPointer(const int R, const int C, int a[R][C]);

int main() 
{
  //creating 2D array
  const int R=2;
  const int C=2;
  int A[R][C] = {{2,9}, {6,4}};

  fillArrayPointer(R, C, A[][C]);
}

int fillArrayPointer(const int R, const int C, int a[R][C])
{
//creating 1D runtime array and putting all values from 2D array in it
  int* B= new int [R*C];
  for (int i=0; i<R; i++)
  {
    for (int j=0; j<C; j++)
    {
      int a=0;
      B[a]=A[i][j];
      a++;
    }
  }
}
Last edited on
You passing it incorrectly. It would be:

fillArrayPointer(R, C, A);

But yoiu cannot pass a 2d array where all dimensions are unknown at compiletime anyway. See:
https://www.tutorialspoint.com/Passing-two-dimensional-array-to-a-Cplusplus-function
First, please use code tags. See http://www.cplusplus.com/articles/jEywvCM9/
* Syntax highlight makes reading easier
* Line numbering helps commenting
* Preservation of (systematic) indentation helps to spot logical errors
* There is even a convenient shortcut to online compiler

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

int fillArrayPointer(const int R, const int C, int a[R][C]);

int main()
{
  //creating 2D array
  const int R=2;
  const int C=2;
  int A[R][C] = {{2,9}, {6,4}};

  fillArrayPointer(R, C, A[][C]);
}

int fillArrayPointer(const int R, const int C, int a[R][C])
{
  //creating 1D runtime array and putting all values from 2D array in it
  int* B= new int [R*C];
  for (int i=0; i<R; i++)
  {
    for (int j=0; j<C; j++)
    {
      int a=0;
      B[a]=A[R][C];
      a++;
    }
  }
}

4:55: error: use of parameter outside function body before ']' token
4:58: error: use of parameter outside function body before ']' token
 In function 'int main()':
13:28: error: expected primary-expression before ']' token
 At global scope:
16:55: error: use of parameter outside function body before ']' token
16:58: error: use of parameter outside function body before ']' token
 In function 'int fillArrayPointer(...)':
19:20: error: 'R' was not declared in this scope
19:22: error: 'C' was not declared in this scope
25:12: error: 'A' was not declared in this scope
29:1: warning: control reaches end of non-void function [-Wreturn-type]



int fillArrayPointer(const int R, const int C, int a[R][C]);
error: use of parameter outside function body before ']' token

What is the size of the array?
It must be known during compilation (except the major dimension).

1
2
constexpr int C {2}; // global constant
int fillArrayPointer( int rows, int a[][C]);



1
2
3
  const int R=2;
  int A[R][C] = {{2,9}, {6,4}};
  fillArrayPointer( R, C, A[][C] );

error: expected primary-expression before ']' token
1
2
3
4
5
6
7
8
A       // is the whole array
A[1]    // is the second row of the array. An array of int
A[1][0] // is one int

// hence:
  constexpr int R {2};
  int A[R][C] {{2,9}, {6,4}};
  fillArrayPointer( R, A );



1
2
3
4
5
6
7
8
9
10
11
12
13
14
int fillArrayPointer( int rows, int a[][C] )
{
  //creating 1D runtime array and putting all values from 2D array in it
  int* B= new int [rows*C];
  for (int i=0; i<rows; i++)
  {
    for (int j=0; j<C; j++)
    {
      int a=0;
      B[a]=A[rows][C];
      a++;
    }
  }
}

 In function 'int fillArrayPointer(int, int (*)[2])':
1:12: error: 'A' was not declared in this scope
 At global scope:
10:42: warning: unused parameter 'a' [-Wunused-parameter]
 In function 'int fillArrayPointer(int, int (*)[2])':
14:1: warning: control reaches end of non-void function [-Wreturn-type]

* You create dynamic array B on row 4 but you neither delete nor return it.
* You declare int a on line 9 that masks parameter int a[][C] from line 1
* You declare int a inside loop, so effectively the loop is:
1
2
3
4
for (int j=0; j<C; j++)
{
  B[0] = A[R][C];
}

* Wait, the R and C are effectively constants. The loop is:
1
2
3
4
for (int j=0; j<C; j++)
{
  B[0] = A[2][2];
}

But the array does not have element A[2][2], nor row A[2]. The last valid element of 2*2 matrix is A[1][1].
* Finally, your function should return one int value. It does not.
Is that what you are trying to achieve?

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
#include <iostream>

template<size_t R, size_t C>
int* fillArrayPointer(const int (&a)[R][C])
{
	//creating 1D runtime array and putting all values from 2D array in it
	int *B {new int[R * C]}, *D {B};

	for (int i = 0; i < R; ++i)
		for (int j = 0; j < C; ++j)
			*D++ = a[i][j];

	return B;
}

int main()
{
	const int R {2};
	const int C {2};
	const int A[R][C] {{2,9}, {6,4}};
	const int* const D {fillArrayPointer(A)};

	for (size_t i = 0; i < R * C; ++i)
		std::cout << D[i] << "  ";

	std::cout << '\n';

	delete[] D;
}



2  9  6  4

Thank you for all you help guys,

I just wanted to know the correct way to call a 2D array, but I'm just very confused now :( There are just some things I don't understand in your code, like lines 4, 7, and 21 in seeplus's code. Maybe it's because I haven't gotten that far in c++ :/

Please just tell me how I can call the function fillArrayPointer in the simplest way possible
Lines 3 & 4 are a way of obtaining the dimension sizes of an array without having to specify these as separate function parameters. R & C are the dimension sizes. This way means that to call the function in the simplest way possible all you need to do is to specify the array variable - as per L21

What don't you understand about L7 and L21?

L7 B is a pointer to int which is initialised from the value of new int [R * C] which returns a pointer to memory of size R*C elements. D is also a pointer to int which is initialised to the value of B already obtained. D is used in setting the elements of the 1d array.

L21 D is a const pointer to const data which is initialised by the returned value from fillArrayPointer(A)
Thank you so much!
The fillArray can also be done simpler without using nested for loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstring>

template<size_t R, size_t C>
int* fillArrayPointer(const int (&a)[R][C])
{
	//creating 1D runtime array and putting all values from 2D array in it
	return reinterpret_cast<int*>(memcpy(new int[R * C], &a, R * C * sizeof(int)));
}

int main()
{
	const int R {2};
	const int C {2};
	const int A[R][C] {{2,9}, {6,4}};
	const int* const D {fillArrayPointer(A)};

	for (size_t i = 0; i < R * C; ++i)
		std::cout << D[i] << "  ";

	std::cout << '\n';

	delete[] D;
}


However, rather than dealing with raw-pointers, a better C++ way would be to use a managed pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstring>
#include <memory>

template<size_t R, size_t C>
auto fillArrayPointer(const int (&a)[R][C])
{
	//creating 1D runtime array and putting all values from 2D array in it
	return std::unique_ptr<int[]> (reinterpret_cast<int*>(memcpy(new int[R * C], &a, R * C * sizeof(int))));
}

int main()
{
	const int R {2};
	const int C {2};
	const int A[R][C] {{2,9}, {6,4}};
	auto D {fillArrayPointer(A)};

	for (size_t i = 0; i < R * C; ++i)
		std::cout << D[i] << "  ";

	std::cout << '\n';
}

Topic archived. No new replies allowed.