0

Is there a C/C++ equivalent to C# params?

This

C#:

public static void UseParams(params int[] list) {
  for (int i = 0; i < list.Length; i++) {
    Console.Write(list[i] + " ");
  }
  Console.WriteLine();
}
UseParams(1, 2, 3, 4); // UseParams(int ..........)

I need to do it in C/C++:

void updateJsonArray_INA(INA3221DATA _channel1, INA3221DATA _channel2, INA3221DATA _channel3) {

}

Can I do it like this?

void updateJsonArray_INA(INA3221DATA _channel1, _channel2, _channel3) {

}
dda
  • 6,030
  • 2
  • 25
  • 34
Nicky
  • 87
  • 1
  • 13
  • 1
    Search for 'variadic functions' (http://en.cppreference.com/w/cpp/utility/variadic). Well known functions such as `printf` work in this way – LordWilmore Mar 22 '18 at 16:28
  • 1
    `I need to do it in C/C++:` c or c++? These are 2 different languages. `c++` would be a different answer than `c` – drescherjm Mar 22 '18 at 16:30
  • good old fashioned varargs - https://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic-Example – pm100 Mar 22 '18 at 16:32
  • @drescherjm AFAIK C is a valid subset of C++, so an answer that works in the former will be valid in the latter – LordWilmore Mar 22 '18 at 16:33
  • I dont know, is xtensa compiler in c or c++ (arduino)? Lets say that its c++. Also it is maybe possible duplicate, i just couldn't find it. I will try it then i will be back with answer did it work or no. – Nicky Mar 22 '18 at 16:33
  • 4
    @LordWilmore C is not a valid subset of C++. C and C++ share a lot of things but there are things in C you can't do in C++. As the languages age the gap is becoming wider. – NathanOliver Mar 22 '18 at 16:37
  • 1
    `void updateJsonArray_INA(const std::vector& channels)` seems more idiomatic. Usage would be `updateJsonArray_INA({1, 2, 3})`. – Jarod42 Mar 22 '18 at 16:37
  • @Jarod42 so how to use vector then inside void? i dont know c and c++ well. can i do it channels[0]... or channels<0>? – Nicky Mar 22 '18 at 16:47
  • @Nicky ***how to use vector then inside void?*** In modern `c++` use `for( const auto & channel : channels) { // do something with channel }` – drescherjm Mar 22 '18 at 18:00
  • @NathanOliver Thanks for the tip-off, seems you learn something new every day (https://stackoverflow.com/questions/1201593/where-is-c-not-a-subset-of-c) – LordWilmore Mar 23 '18 at 08:10

1 Answers1

4

In C, there is a feature called Variadic functions. This feature is almost equivalent to C# params except that you have to give the params size or something like that, which is because of the nature of low level array in C.

#include <stdio.h>
#include <stdarg.h>

void use_params(int number_of_params, ...) {
    va_list args;
    va_start(args, number_of_params);

    for(int i = 0; i < number_of_params; ++i) {
        some_type element = va_arg(args, some_type);
        do_something_with(element);
    }

    va_end(args);
}

void main() {
    use_params(4, 'a', 'b', 'c', 'd');
}

This way is the best option about either performance or flexibility (you can have multiple params of multiple types).

But if you do not love using va_list, va_start,... and prefer something easier to read, std::vector is where to go. Note that std::vector is a C++ feature, so it will be not available if your system supports only C, in which variadic functions is the only choice.

#include <iostream>
#include <vector>

void use_params(const std::vector<int>& params) {
    for(size_t i = 0; i < params.size(); ++i) {
        do_something_with(params[i]);
    }
}

void main() {
    use_params({1, 2, 3, 5});
}
phuctm97
  • 136
  • 1
  • 9