This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
variable length arrays as arguments to functions
- From: "Maarten Speekenbrink" <M dot Speekenbrink at uva dot nl>
- To: <gcc-help at gnu dot org>
- Date: Tue, 3 Jun 2003 10:28:36 +0200
- Subject: variable length arrays as arguments to functions
I am trying to write a function in c++ that takes a square matrix of variable dimension as its argument. I read in the manual that this should be possible in g++. However, I am experiencing some trouble. I am a novice in c++, so it could be in my code or in the compiler. For instance, when I try the code below, I get an error for the function declaration that N is not specified. Is there a way to write functions for variable-size two dimensional arrays? (I am using the Mingw port of g++).
Thanks for your help,
Maarten
// Program
#include <iostream>
using namespace std;
void MyFunc(int N, float my_matrix[N][N])
int main()
{
float Q[5][5] = {
{0.90, 0.85, 0.70, 0.85, 0.70},
{0.85, 0.90, 0.70, 0.85, 0.70},
{0.60, 0.60, 0.90, 0.60, 0.60},
{0.60, 0.60, 0.60, 0.90, 0.60},
{0.50, 0.50, 0.50, 0.50, 0.50}
};
MyFunc(5, Q);
return 0;
}
void MyFunc(int N, float my_matrix[N][N])
{
for (int i=0; i<N; i++){
float sum = 0;
for (int j=0; j<N; j++) {
sum = sum + Q[i][j];
}
cout << "Rowsum is: " << sum << '\n';
}
}
---------------------------------------------------------------------
Maarten Speekenbrink
Psychological Methodology
Department of Psychology, Faculty of Social and Behavioral Sciences
address: Roetersstraat 15, 1018 WB Amsterdam, Netherlands
tel: +31 20 525 6876 / +31 20 525 6870
fax: +31 20 639 0026
---------------------------------------------------------------------