0
int arr[2][6] = {{0,0,0,1,1,1},
                 {2,2,2,3,3,3}};

for(int row=0;row<2;row++)
{
    for(int col=0;col<6;col++)
    {
        int temp[] = arr[row][col+4],arr[row][col+5],arr[row+1][col],arr[row+1][col+1];
        for(int i=0;i<1;i++)
        {
            cout << temp[i] << endl;
            //Replace 4 values in arr[] with values from temp[]
        }
    }
}

I'm trying to create a smaller temporary array to pull values from when updating the original 2D array. I'm getting an error with the line starting with int temp[]. The error reads:

initialization with '{...}' expected for aggregate object

I'm not sure what's going wrong here, I am a beginner with C++.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Side note: `arr[row][col+4]` will be fatal as soon as `col` reaches 2. It might not be immediately visibly fatal, but the program's not healthy as soon as you waltz out of bounds. – user4581301 Mar 03 '22 at 01:08

1 Answers1

4

You are trying to initialize an int[] array with single int. You are missing curly braces around the array's initialization values (see the braces on your other array), eg:

int temp[] = { arr[row][col+4], arr[row][col+5], arr[row+1][col], arr[row+1][col+1] };
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1 1 2 2 2 3 3 3 1 0 46 0 – wilson_5.0h Mar 03 '22 at 01:13
  • @wilson_5.0h and, your point is... what? – Remy Lebeau Mar 03 '22 at 01:14
  • Thank you, that fixed the error I had. Above was my output for the smaller array created, does this look correct based on what I have? I was hoping for an output of "1 1 2 2". – wilson_5.0h Mar 03 '22 at 01:15
  • 1
    That's your output? Not surprising. Look at the dimension of `arr`, then look at how you're indexing it. If the loop variables `row` and `col` are non-zero, you will fly off the end of the array into uncharted territory. Note that you're rebuilding this array every time (12 times in total) and only outputting the first element. Does that make sense to you? It doesn't make sense to me. – paddy Mar 03 '22 at 01:15
  • Like I said above, going out of bounds may not be immediately visibly fatal, but the program [is not healthy](https://en.cppreference.com/w/cpp/language/ub). In your case unhealthy meant odd output. You got lucky. You could have been visited by an assassin robot from the future looking for Sarah Connor. – user4581301 Mar 03 '22 at 01:21
  • So instead of writing "row+x" and "col+x", I should just insert their actual integer values? – wilson_5.0h Mar 03 '22 at 01:22
  • Depends on the behaviour you want. If you want a simple wrap-around, 4->5->0->1->..., the modulo operator, `(col+4)%6`, is what you need. If the logic is more complicated maybe a function is required. Hard to give advice on exactly what to do (other than [avoid magic numbers](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad)) without an example of the output you require from your given input. – user4581301 Mar 03 '22 at 01:42