Exporting an array from a DLL implemented in Zig


I would need to implement a function that returns an array (or a similar data structure) using Zig and make it available in a DLL.

All my attempts resulted in similar errors:
not allowed in function with calling convention β€˜C’

Is there a way to implement a function that does not return a scalar?

Thank you

The export keyword causes functions to be exported with the C ABI for other programming languages to call into.
A C function cannot return a value of array type (though it can return structures containing arrays).

My recommendation is:

  • if your function must be called from other programming languages, pass as parameters the size and the pointer to a preallocated array by the caller.
  • if your function must be called only from zig don’t use a DLL.

Thank you for your feedback.

I need to call the DLL from other programming languages and I am trying to find a way to return values other than integers.

Perhaps the simplest way would be to convert the array to a null-terminated string, with values separated by a |. This would allow me to process the return value and reconstruct the array in the other language.

Thank you again.

You can return a pointer to anything (including an array).
It is important to decide who owns the memory, the memory owner must have a way to free the memory.