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.