0

I need to evaluate function with list of arguments from array of arguments as in this example:

int compute(...) {
    int n;
    va_list params;
    va_start(params, n);
    // some custom computation with no "va" output
    va_end(params);
}

And some array of int (which is dynamic array, don't rely on fixed size):

int arr[10] = {0, 1, 3, 7, 8, 1, 3, 5, 7, 9};

And I need to call compute like JS function compute.apply(this, arr)

I'm implementing some library with C that's why I need it.

In C++ this is std::apply but I want the same in C.

Thanks

12
  • So ? What is your question exactly ? Commented Oct 15, 2019 at 6:51
  • Hm. Variadic functions are probably not what you need here. If you want to operate on an array of n elements, write a function that takes that array and its length as parameters: int compute(const int *arr, size_t n) . Commented Oct 15, 2019 at 6:51
  • @GuillaumePetitjean I need something like std::apply from C++ in C Commented Oct 15, 2019 at 6:53
  • 2
    It's not whether you like it or not, it's whether C can do it or not. Also, you didn't demonstrate anything. How do you want to call your proposed compute function with the given array? Commented Oct 15, 2019 at 7:01
  • 1
    If your arrays are of a homogenous type, you can use the raw pointer + byte size approach of qsort like this. If your arrays can be heterogenous, you can create a variant type as a tagged union like this. Note how both approaches still must manage their memory explicitly. Is that what you are looking for? Commented Oct 15, 2019 at 8:39

1 Answer 1

1

In C++ this is std::apply but I want the same in C.

Since you want the same in C, you'll surely accept that you have to fulfill the same requirements, in particular, as a tuple supports std::get and std::tuple_size, their C equivalents. Now, as long as the arguments from the array are accessed in order from first to last, std::get can be implemented with va_…(), but just as the stdarg variable argument lists need some means of determining the amount of arguments (like a format string or an argument count), std::tuple_size cannot be implemented without such a means. You won't do without passing this information.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I decided just move project to C++ since I can combine C and C++ features. I also need to run a function in a separate thread for example... C++ is the best with this. But it looks like you're the only person who got my question. Idk why nobody else wants the same feature in C...
I can well imagine people wanting the same feature in C, it's just that C as it is doesn't provide a means of implicitly passing the needed size information, and there are high barriers for a language standard change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.