Why do function variables require at least two arguments?

I am trying to connect a hole in my knowledge. Why do function variables require at least two arguments? Mostly from a C main function having argc as an argument and then argv as an array of character arrays? In addition, Cocoa's Objective-C has NSString methods that require formatting as the first argument, and then an array of arguments ( [NSString stringWithFormat:@"%@", foo] ). Why is it impossible to create a variational function that takes only a list of arguments?

+6
source share
5 answers

The argc / argv file is not really a variation file.

Variadic functions (such as printf() ) use arguments placed on the stack and do not require at least two arguments, but 1.

You have void foo(char const * fmt, ...) and usually fmt gives an idea of ​​the number of arguments. This minimum is 1 argument (fmt).

+7
source

C has very limited reflection capabilities, so you should have some way to specify what it is that contains variable arguments β€” either specify the number of arguments, or the type of them (or both), and this is logic, more than a parameter. This is required by the ISO C standard, so you cannot omit it. If you feel that you do not need additional parameters, because the number and type of arguments are always constant, first of all there is no need for variable arguments.

Of course, you could develop other ways to encode quantity / type information inside variable arguments, such as the sentinel value . If you want to do this, you can simply specify a dummy value for the first argument and not use it in the body of the method.

And just to be pedantic about your name, function variables require only one argument (not two). This is true for invoking the variation function without providing any optional arguments:

 printf("Hello world"); 
+3
source

I think the reason is this: in the macro va_start(list, param); you specify the last fixed argument - you must determine the address of the beginning of the list of variable arguments on the stack.

+1
source

How would you know if a user was provided with any arguments?

There should be some information to indicate this, and C was not intended to process data behind you at all. So, all you need is doing it explicitly.

0
source

I am sure that if you really want you to try to apply some scheme in which the variational function accepts only a certain type of parameter (for example, a list of ints) - and then you fill in some global variable indicating how many ints you have passed.

Your two examples are not variational functions. These are functions with two arguments, but they also highlight a similar problem. How to find out the size of a C array without additional information? You can either pass the size of the array, or describe a circuit with a number of sentinel values, unmapping the end of the array (ie "\ 0" for string C).

As in the case of the variational register, and in the case of the array, you have the same problem, how can you find out how much data you have legal access? If you do not know this in the case of an array, you will go beyond. If you do not know this in the variational case, you will call va_arg too many times or with the wrong type.

To include the question, how would you implement a function with a variable number of arguments without passing additional information?

0
source

Source: https://habr.com/ru/post/898476/


All Articles