What is the easiest way to set the value of an entire array?

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-06-09T22:20:02Z Indexed on 2010/06/09 22:22 UTC
Read the original article Hit count: 107

Filed under:
|
|

My current project requires me to fill an array based upon some other values. I know there's the shortcut:

int arr[4][4] = { {0,0,0,0} , {0,0,0,0} , {0,0,0,0} , {0,0,0,0} };

But in this case, I need to fill the array after its declaration. I currently have my code formatted like this:

int arr[4][4];
if(someothervariable == 1){
    arr = { {1,1,1,1},
            {1,2,3,4},
            {2,,3,4,5},
            {3,4,5,6} };
}

But it won't compile. Is there a way to make use of the mentioned shortcut in my case? If not, whats the best fix available? I'd appreciate a way to set it without explicitly assigning each element? ie: arr[0][0] = ...

© Stack Overflow or respective owner

Related posts about c++

Related posts about array