Shortest way to allocate an array and initialize it with a specific value.
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jun 10 15:03:52 PDT 2015
On 06/10/2015 01:22 PM, Adel Mamin wrote:
> ubyte[5] a = 0xAA; // Fine. Five 0xAA bytes.
> auto a2 = new ubyte[5]; // Fine. Five 0 bytes.
> Now, let's say, I want to allocate an array of a size, derived at run
> time, and initialize it to some non-zero value at the same time. What
> would be the shortest way of doing it?
Another option:
void main()
{
auto a2 = new ubyte[5];
a2[] = 0xAA; // <-- Assign to "all elements"
/* Alternative syntax:
*
* auto a2 = new ubyte[](5);
*/
}
Ali
More information about the Digitalmars-d-learn
mailing list