declaring arrays

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Randi
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Sat Apr 24, 2010 1:32 pm
Location: Noobville

declaring arrays

Post 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?
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: declaring arrays

Post 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
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: declaring arrays

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply