1

EDIT

Thanks to @crashmstr, he showed the error. I forgot to add const to ROW and COL, VLAs were the reason for the 'errors'.

This is literally all the code:

#include <iostream>

int main()
{
    int ROW = 3;
    int COL = 4;

    int arr[ROW][COL] = { { 5 } };

    for(int row = 0; row < ROW; row++)
    {
        for(int col = 0; col < COL; col++)
        {
            std::cout << arr[row][col] << '\t';
        }
        std::cout << '\n';
    }

    return 0;   
}

This is the output:

5   4199120 4201168 0   
0   0   0   0   
0   0   0   0

And this was my expected output:

5   0   0   0   
0   0   0   0   
0   0   0   0

My Question: Why is this happening? When you initialize a 1D array int arr[3] { 5 }; the first element is set to 5 and everything else to 0, but when I do the 'same' with the 2D array I get this

Marat Isaw
  • 131
  • 3
  • 10

0 Answers0