View Single Post
Old Apr 16th, 2008, 3:48 PM   #4
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,007
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Init array with a non-zero value

Quote:
Originally Posted by kurt
This is embarrasing, but I've always thought:

int variable[10] = {9};

will init all items to 9. But it does not.
When you initialize an array like that, it will initialize all elements you specify to the values you give, and any remaining elements will be initialized to zero (for fundamental types) or with the default constructor (for objects). If the array does not have a subscript, then the number of elements is taken from the number of values you provide:
C++ Syntax (Toggle Plain Text)
  1. // all set to 9
  2. int array1[10] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
  3.  
  4. // first three set to 9, rest set to 0
  5. int array2[10] = {9, 9, 9};
  6.  
  7. // five elements, all set to 9
  8. int array3[] = {9, 9, 9, 9, 9};
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote