Thread overview
Syntax Sugar for Initializing a Fixed float Array as void*?
December 01

Is there a way to write a single statement that creates a void pointer that points to an initialized float array? See below:

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;
December 01

On Thursday, 1 December 2022 at 00:39:21 UTC, 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?

    void* f = [1.0f, 1.0f, 1.0f].ptr;

Though I'd recommend keeping it typed as float[] until the last possible moment. If you are passing it a function, remmeber pointers convert to void* automatically, so you can do like:

float[] f = [1,1,1];

some_function_taking_void(f.ptr);

and it just works.

November 30
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

December 01

On Thursday, 1 December 2022 at 00:47:18 UTC, Adam D Ruppe wrote:

>

On Thursday, 1 December 2022 at 00:39:21 UTC, 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?

>

float[] f = [1,1,1];

some_function_taking_void(f.ptr);

and it just works.

Thank you, that was just the magic I needed!

float[4] arr = [0.1f, 0.1f, 0.1f, 1.0f]; // Init array with values

SetShaderValue(
    shader, ambientLoc, arr.ptr, ShaderUniformDataType.SHADER_UNIFORM_VEC4
); //                   ^^^^^^^---void* here
November 30
On 11/30/22 16:48, Ali Çehreli wrote:

> Functions are syntax sugar. :)

And I remembered std.array.staticArray. One its overloads should be useful:

import std;

void main() {
    auto v3 = staticArray!(0.1f.repeat(5));
    auto v4 = staticArray!5(0.1f.repeat);

    writeln(v3);
    writeln(v4);
}

Ali