Page 1 of 1

declaring arrays

Posted: Mon May 24, 2010 9:16 pm
by Randi
I am reading a c++ book, and it doesn't go into detail on how to change the whole array after it has been declared for example myArray[10] {0,1,2,3,4,5,6,7,8,9} wont work, how is this done?

Re: declaring arrays

Posted: Mon May 24, 2010 9:40 pm
by davidthefat
Randi wrote:I am reading a c++ book, and it doesn't go into detail on how to change the whole array after it has been declared for example myArray[10] {0,1,2,3,4,5,6,7,8,9} wont work, how is this done?
Example:

Code: Select all

int Array[10]; //Declares an array with 10 ints
Array[0] = 12; //Arrays start with 0 not 1
you can do a for loop and go change each element in the array I never really used the {} method, I like the loops better

Re: declaring arrays

Posted: Tue May 25, 2010 8:49 am
by dandymcgee
Initialization lists can only be used when you first declare the array. If you want to set the entire contents of the array you have to either use a loop as david suggested, or initialize another array with the desired contents and assign it to the original array.