variable length arrays as arguments to functions

qinfeng.zhang@philips.com qinfeng.zhang@philips.com
Tue Jun 3 10:36:00 GMT 2003


Maybe C++ don't supports dynamic array. since you defined the array "my_matrix" with undefined range, the programm is error.
I think you can defined the array using pointer as "float **my_matrix"


                                                                                                                                                                                                               
                                                                                                                                                                                                               
                                                   To:   <gcc-help@gnu.org>                                                                                                                                    
                                                   cc:   (bcc: Qinfeng Zhang/SHA/RESEARCH/PHILIPS)                                                                                                             
                                                   Subject:    variable length arrays as arguments to functions                                                                                                
                                                                                                                                                                                                               
               "Maarten Speekenbrink"              Classification:                                                                                                                                             
               <M.Speekenbrink@uva.nl>                                                                                                                                                                         
                                                                                                                                                                                                               
               Sent by:                                                                                                                                                                                        
               gcc-help-owner@gcc.gnu.o                                                                                                                                                                        
               rg                                                                                                                                                                                              
                                                                                                                                                                                                               
               2003-06-03 16:28                                                                                                                                                                                
                                                                                                                                                                                                               
                                                                                                                                                                                                               




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
---------------------------------------------------------------------







More information about the Gcc-help mailing list