I was following internet tutorials on this topic, but I have the following situation:
I have a function with the following signature:
void func(long& rows, long& columns, int array[][columns]);
and I'm trying to use the function like this:
int matrix[5][4] = {0, -1, 2, -3,
4, -5, 6, -7,
8, -9, 10, -11,
12, -13, 14, -15,
16, -17, 18, -19};
long rows = 5;
long columns = 4;
func(rows, columns, matrix);
^--- 'No matching function for call to 'func''
What is the problem? Why can't it call the function?
CodePudding user response:
Variable length arrays is not a standard C features.
You could declare the function and the array the following way
const size_t columns = 4;
void func( size_t rows, const int array[][columns]);
//...
int matrix[][columns] = { { 0, -1, 2, -3 },
{ 4, -5, 6, -7 },
{ 8, -9, 10, -11 },
{ 12, -13, 14, -15 },
{ 16, -17, 18, -19 } };
func( sizeof( matrix ) / sizeof( *matrix ), matrix);
//...
void func( size_t rows, const int array[][columns] )
{
std::cout << rows << columns << array[0][1];
}
Pay attention to that as the number of columns is well-known there is no sense to pass it to the function. And moreover there is no sense to pass the number of rows and columns by reference.
CodePudding user response:
Have you actually defined func
in your program?
The following source code compiles and works fine for me
#include <iostream>
#define ROW 5
#define COLUMN 4
void func(long &rows, long &columns, int array[][COLUMN]);
int main()
{
int matrix[ROW][COLUMN] = {0, -1, 2, -3,
4, -5, 6, -7,
8, -9, 10, -11,
12, -13, 14, -15,
16, -17, 18, -19};
long rows = 5;
long columns = 4;
func(rows, columns, matrix);
return 0;
}
void func(long &rows, long &columns, int array[][COLUMN])
{
std::cout << rows << columns << array[0][1];
}