0

I am using a library function that expects an array of pointers (void**) at some point, it works more or less like this.

void* args[] = { &var_a, &var_b, &var_c, ... };
someFunction(args);

After using it once, I would like to call the same function again, so what I do is create another variable like:

void* args_2[] = { &var_d, &var_e, &var_f, ... };
someFunction(args_2);

And so on ...

I would like to find a way to recycle the args symbol, so I don't have to do args_2, args_3, args_4 every time I call it; but when I try to reassign it like:

args = { &var_d, &var_e, &var_f, ... };

I get the following:

error: assigning to an array from an initializer list

I understand the error but I don't know how to avoid it or coerce this thing into the intended array of pointers type.

I know they are two different languages, but I am looking for a solution that works in both C and C++.

10
  • 4
    C or C++? There is a difference. Commented Oct 25, 2019 at 12:23
  • I could accept an answer that works in both scenarios. Also, @NathanOliver could you tell me more about what could be different? Commented Oct 25, 2019 at 12:24
  • Use a std::vector<void *>, that does support construction from an initializer list. Use as someFunction(args.data()); Commented Oct 25, 2019 at 12:28
  • 1
    If you want solutions for both C or C++, I suggest asking two separate questions, as the solutions are inherently different (C can do things C++ can't and vice versa). If you don't care, then I recommend just sticking with one language. Roll a dice if you must, but I suggest C++ as you can simply use std::vector Commented Oct 25, 2019 at 12:28
  • 4
    @almosnow well, no, that's a myth. kiran's answer is C-only, and mine is C++-only (even if you use typedef). What now? Commented Oct 25, 2019 at 12:41

2 Answers 2

3

Why use a local variable at all, if you only need it once to call someFunction?

using args = void *[];

someFunction(args{&a, &b});
someFunction(args{&a, &b, &c});

Alternatively, C++ify this a bit more with a wrapper:

template <class... T>
decltype(auto) someFunction(T *... args) {
    void *args_[] { args... };
    return someFunction(args_);
}

someFunction(&a, &b);
someFunction(&a, &b, &c);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Quentin, I'm just testing it quickly, will be back at this.
1

You could make use of compound literals as below with pointer to pointer.

void** args = (void *[]){ &var_a, &var_b, &var_c, ... };

args = (void *[]){ &var_d, &var_e, &var_f, ... };

2 Comments

Thank Kiran! In spirit I am looking for something like this, however, it does not compile: due to "error: taking address of temporary array". Which I understand as well, hmm, how could this be avoided?
@almosnow this might help you stackoverflow.com/questions/32941846/….

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.