I have a function:
vector<int> prime(int num, ...) {
vector<int> mas;
va_list args;
va_start(args, num);
for (int i = 0; i < num; i++) {
int v = va_arg(args,int);
if (isPrime(v)){
mas.push_back(v);
}
}
cout << endl;
va_end(args);
return mas;}
It should detected prime numbers. But when i call it, part of my numbers, don`t get over.
It looks something like this
Input: 5, 7, 10, 15, 20,12, 13,16,19
Numbers what cout returns in the loop: 7,7
Help pls!
va_arg(args, int)3*n times. How is it supposed to know that the first three calls should return the first argument, the second three should return the second argument, and so on? It burns through all arguments in n/3 iterations, and then UB happens. You need to call it one per iteration, and save the result to a variable.const std::vector<int> &as a parameter (or, in C++20,std::span<const int>), or, if you absolutely want the same call syntax you have now, use variadic templates.