How do I return a C array of strings to be used in Vala code?

56 Views Asked by At

I'm using Vala to create a Linux app.

To parse JSON, I want to use C, because Vala library is complicated for me and, and it is able to run C functions. The function should return an array of strings that will be used inside Vala code.

The main problem is that unlike Vala, C does not really know what is an array of strings is. I found that the array of strings can be declared as a char*[].

I tried to:

  • Use a returned char*[] as an array of strings in Vala, but that did not help.
  • Convert returned char* [] from C to a string array in Vala directly, but I was given a lot of compilation errors.

I've made an assumption that char*[] in C, and char*[] in Vala, work differently.

1

There are 1 best solutions below

0
nemequ On

It depends on the what you want do; Vala is actually quite flexible in how it binds to C APIs since it was designed to be used with C APIs which don't know anything about Vala.

My suggestion would be to write the API you want in Vala, then use valac -C foo.vala to transpile the Vala to C code, where you can easily see the API it expects. Basically, though, for a Vala API like string[] foo(), the expected C signature would be something like gchar** foo(gint* array_length).