You can use compound literal for that. Here is handy macro:
#define INIT(array, type, nelem, ...) memcpy(array, (type []){__VA_ARGS__}, nelem * sizeof(type))
Example usage:
float *foo(void)
{
float *f = malloc(5 * sizeof(*f));
INIT(f, float, 5, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f);
return f;
}
Optimizing compilers will get rid of the compound literal and malloc
call generating some optimized code:
foo:
push rax
mov edi, 20
call malloc
movabs rdx, 4611686019492741120
movabs rcx, 4647714816524288000
mov QWORD PTR [rax], rdx
mov QWORD PTR [rax+8], rcx
mov DWORD PTR [rax+16], 0x40a00000
pop rdx
ret