Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable length argument list of set type

Alright, I'm pretty sure that this has been discussed before in some way or another, but I'm apparently too stupid to find it.

First: I'm NOT looking for va_list and the other macros. What I am looking for is something like the main-function parameters.

The default prototype, as all of you know is:

int main(int argc, char *argv[]);

Now, I want something similar for my program, but don't know how exactly.

Let's assume we have this function:

void Function(int argc, unsigned short *args[]) {
    for(int i = 0; i < argc; ++i) printf("%hu ", args[i]);
}

And I want something like this function call:

Function(5, 1, 2, 3, 4, 5);

Would that work? Because I don't want the 'cluttering' of va_list, nor do I want to create:

void AnotherFunction() {
    unsigned short Args[] = { 1, 2, 3, 4, 5 };
    Function(5, Args);
}

Simply because in that case I would only need a simple pointer. Could someone please point me in the right direction? Thank you very much.

Edit: Thank you everyone for your valuable input. I'll settle for 'Doesn't work with standard C/C++ for now and look for a different approach to my problem.

Again, thank you very much.

like image 269
ATaylor Avatar asked Oct 07 '12 18:10

ATaylor


People also ask

What are variable length argument with types?

Variable-length arguments, abbreviated as varargs, are defined as arguments that can also accept an unlimited amount of data as input. The developer doesn't have to wrap the data in a list or any other sequence while using them. Let's understand the concept with the help of an example.

What is variable length argument list in Java?

Variable-length Argument Lists in Java 5 Java 5 (and hence AspectJ 5) allows you to specify methods that take a variable number of arguments of a specified type. This is achieved using an ellipsis (...) in the method signature as shown: public void foo(int i, String... strings) { }

Which of the following is used for variable length argument?

Explanation: In python, * is used to send the variable length argument list ( i.e., no. of arguments intake at a time ) to a function.

What is variable length parameter list?

Variable-length argument lists are a new feature in J2SE 5.0. Programmers can create methods that receive an unspecified number of arguments. An argument type followed by an ellipsis (...) in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.


4 Answers

A g++ specific solution:

const size_t MAX_ARGS = 42;

void Function(int argc, const unsigned short (&args)[MAX_ARGS])
{
}

int main()
{
    Function(5, {1,2,3,4,5});
}

If you don't actually need the argc parameter, you can write a template:

template<size_t N>
void Function(const unsigned short(&args)[N])
{
    // use N
}

This works in C++03. The array needs to be const, or else you can't pass a temporary initializer list like that. If you need to modify the elements inside the function, you'll need to make a copy.

like image 150
jrok Avatar answered Oct 27 '22 03:10

jrok


Since you also tagged with C: in C you can use a macro combined with a so-called compound literal

#define  FUNCTION(ARGC, ...) (Function((ARGC), (unsigned short[]){ __VA_ARGS__ }))

This is type safer that the acient va_list functions, since it converts all the expressions in the list to unsigned short.

Modern C++ now also has this VA_ARGS feature for the macro preprocessing phase. But I don't know how you would create a vector as a temporary that would be initialized with the values.

like image 27
Jens Gustedt Avatar answered Oct 27 '22 04:10

Jens Gustedt


Make use of default parameters.

Function(int argc, unsigned short *args[], int a = 2, int b = 3, int c = 4) ;

or whatever types you want for yourself. The default values, as are the last three in this example need not be passed to the function. You can call the function like this..

Function (argc, args, 5, 4, 3) ;  //have to remember the maximum no. of parameters

I don't know why you want to do this kind of thing in your program. If you do not need the extra variables, why include them in your program? But

like image 28
Coding Mash Avatar answered Oct 27 '22 02:10

Coding Mash


To write a function like that in C, you need to va_list and other macros. This stems from the conversion from C to assembly. In assembly, you can just "look up" on the stack frame to look at what may be more arguments passed to the function.

However, in C, the way this must be done using the va_list types and macros because there is no other built-in mechanism you could use while still maintaining the function's prototype (the number and types of arguments).

Refer to http://www.gnu.org/software/libc/manual/html_node/Variadic-Functions.html.

like image 33
ryucl0ud Avatar answered Oct 27 '22 02:10

ryucl0ud