www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Syntax Sugar for Initializing a Fixed float Array as void*?

reply jwatson-CO-edu <real.name colorado.edu> writes:
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;
```
Nov 30 2022
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
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.
Nov 30 2022
parent jwatson-CO-edu <real.name colorado.edu> writes:
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! ```d 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 ```
Nov 30 2022
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Nov 30 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Nov 30 2022