Subj : Re: Dynamic Array Initialization To : borland.public.cpp.borlandcpp From : maeder Date : Mon Mar 28 2005 10:47 am "kvgs" writes: > int *Bets; > Bets = new int [10]; Side note: better intialize variables in their definition. It's much easier to get/keep code correct that way. E.g.: int *Bets(new int[10]); Better yet, use a container object, such as a vector. > Bets[10] = {1,2,3,4,5,6,7,8,9,10}; The expression Bets[10] is (an l-value) of type int. First, you can't assign it anything that is not convertible to int. Second, the curly braces syntax on the right side of the assignment operator is only allowed in aggregate initializations. This is an assignment, though, not an initialization. > for (int i=0; i<10; i++) > ShowMessage(IntToStr(Bets[i])); > delete[] Bets; > > If I do this instead and don't use a dynamic array, everything is > ok > > int Bets[10] = {1,2,3,4,5,6,7,8,9,10}; Here you are using aggregate initialization to initialize the array. Aggregate initialization can only be used to initialize, uuh, aggregates. Aggregates include structs, unions, arrays of static and automatic storage duration, but not dynamically allocated arrays. Elements of dynamically allocated arrays can't be initialized. To give them their desired values, you have to assign to them after the allocation. To assign values to the elements of the dynamically allocated array, use either a for loop or an appropriate Standard C++ Library algorithm. .