Click here to Skip to main content
15,847,653 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm working with 2d arrays. At the beginning I would like to set my entire array to zero. I know for a 1d array we use something like this:
int arr[100] = {0};
What would be the shortcut for a 2d one??
Posted

1 solution

When you're initialising an array if you don't specify elements (after the first) they're zero initialised. So all we have to do is specify the first element of the outer array:

C++
int arr[10][10] = {{0}};


and we're done.

Oh, and mandatory lecture about using arrays in C++: Is there any reason you're using an array rather than a vector of vectors? Efficiency of creation is a legitimate answer as creating vectors of vectors is more expensive but when they're built they're just as efficient in most cases.
 
Share this answer
 
Comments
Alexander24 20-Nov-13 10:24am    
Honestly I haven't learned how to use vectors yet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900