How can you specify the starting values of an array?
How can you specify the starting values of an array?
Do you mean how do you declare and initialize the values of an array? If so, try something likeDo a similar thing for any other type: type a list of values, separated by commas, and the entire list is surrounded by curly brackets.Code:int foo[5] = {1,2,3,4,5}; // or without the size explicitly, like: int bar[] = {1,2,3,4,5};
Code:int nums[1000] = {0}; // All elements are initialized to 0. int nums[1000] = {1, 2, 3}; // First 3 elements are initialized to 1, 2, & 3, the rest are initialized to 0.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
I just wanted to point out that all the elements are initialized to zero, but not because you did "{0}". You would get the same array if you did "{}". Specifying the values of the array when you declare it allows you to specify some (may be all) values of the array, and all other values are set to 0 (at least for our example). The above notation may be misleading, and someone might dothinking it would initialize all elements to "5", when it just sets the first element to 5 and all others to 0.Code:int nums[1000] = {5};
Let me know if Im incorrect and misunderstanding things. I know I thought that specifying one value initialized the whole array to that value was true in Java--it might actually be, but Ive never verified or looked it up.