Syntax Sugar for Initializing a Fixed float Array as void*?

Ali Çehreli acehreli at yahoo.com
Thu Dec 1 00:48:10 UTC 2022


On 11/30/22 16:39, jwatson-CO-edu wrote:
> Is there a way to write a single statement that creates a void pointer 
> that points to an initialized float array?  See below:
> ```d
> float* arr = cast(float*) new float[4];
> arr[0] = 0.1;
> arr[1] = 0.1;
> arr[2] = 0.1;
> arr[3] = 0.1;
> void* value = cast(void*) arr;
> ```

Functions are syntax sugar. :)

import std;

void* inittedArray(T)(T value, size_t count) {
     auto arr = new T[count];
     arr[] = value;
     return arr.ptr;
}

void main() {
     auto v = inittedArray(0.1f, 5);

     // or something a little crazy:
     auto v2 = 0.1f.repeat(5).array.ptr.to!(void*);
}

Ali



More information about the Digitalmars-d-learn mailing list