Question : pass array to function

Hi ,
what is a wrong in this code. The resuly should be 0,1,2,3,4 but it prints that 18739
why and how can I solve it please ?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
int MainWindow::readFile()
{
    int array [5][0];
    MainWindow::test(array);

}
void MainWindow::test(int array[5][0])
{
    for(int i=0;i<5;i++)
    {
        cout << array[i][0];
    }
}

Answer : pass array to function

Continued: I made some test (with MS Visual Studio - depending on your compiler this may differ):

> int array [5][0];

produces two errors:

> error C2466: cannot allocate an array of constant size 0
> error C2087: 'array' : missing subscript

so using the [0] is definiteley wrong here.

Next I tested this code:

> int MainWindow::readFile()
> {
>      int array[5][1];
>      MainWindow::test( array );
>      return 0; // needed to add this to avoid 'error C4716: 'MainWindow::readFile' : must return a value'
> }
>
> void MainWindow::test( int array[5][1] )
> {
>      for ( int i = 0; i < 5; i++ )
>      {
>            std::cout << array[i][0];
>      }
> }

This leads to output like
> 9588743800752958733958874906623
since the values in the array aren't initialized

I added initialization in 'readFile' like this:
> ...
> int array[5][1] = { 0, 1, 2, 3, 4 };
> ...

Output is '01234' as expected.

ZOPPO
Random Solutions  
 
programming4us programming4us