The c language is based on the use of the variable parameter functions of stdarg.h

  • 2020-05-26 09:53:34
  • OfStack

In the programming of C language, some functions with variable number of parameters are sometimes encountered. This paper explains the implementation principle of variable parameter functions in detail and shares it with you

When we first learned about functions in C, we knew that the number of arguments to a function should be specified when the function is declared. We had no doubt about that. But I don't know if you've noticed our printf() function, whose arguments are not theoretically deterministic, but are controlled by the number of formatting controllers in the matching string. In fact, I did not notice this point at that time. Recently, I happened to read the book "hi translation C language", which explained the principle of the realization of this variable parameter function in detail. I learned 1 during the exam interval today, which is actually a method.

The header file

This usage refers to macros defined in the C standard library "stdarg.h" (C++, of course, is "cstdarg").

Function declaration


int fun(int arg1,int arg2, ...){
//foo
}


In this case, we can pass in several fixed parameters by using ordinary parameter transfer method. In general, there will be a number representing the number of variable parameters (otherwise, the function will not know what you have passed to it). Finally, there will be three consecutive points representing the variable parameters. The mutable parameter that we're passing in is right here.

The specific use

The va_list types, va_start(), va_arg(), va_end() are involved here.

va_list can be understood as holding one data type of those mutable parameters in the form of a linked list (which, of course, is mutable since it is a linked list).

void va_start(va_list ap, int len); Take two parameters, one for the thing above, and one for the number of data. Visual observation can be understood as automatically collecting the variable parameters passed to this function and assigning information to va_list along with its number.

type va_arg(va_list ap, type); Take two arguments, one for va_list and the other for the type of data to be saved. Because the compiler does not check what parameter is actually entered when the argument is called, it needs to be specified in due course and returned with that type. Note that at this point, one of the Pointers in va_list will point to the next element, so the output value on the next call will be the next element. The usage at this point is similar to iterator.

void va_end (va_list ap); Finally, clean up this va_list.

The demo source code


#include<stdarg.h>
#include<stdio.h>
void print(intarg,...){
 va_list ap;
 va_start(ap, arg);
 for (int i = 0; i < arg; i++){
 printf("%d ", va_arg(ap,int));
 }
 va_end(ap);
}
int main(){
 print(4, 1, 2, 3, 4);
}

The result, of course, is 1, 2, 3, 4.

instructions

Although it looks cool, it is actually said to be very unsafe and prone to running errors, so try to use it as little as possible. This kind of problem can be solved with overloading or classes rather than with mutable argument functions.


Related articles: