An application example of indefinite parameter in C language: indefinite parameters as a special skill of the middle-C + + language was praised by many people, but in fact this technology has not been applied much. I don't see much application except formatted output. The main reason is that this technology is more cumbersome, side effects are more, and in general the overloaded function is enough to replace it. However, since you are interested in it, let me briefly summarize its usage and common problems that need attention.
Just learn C language, the General people will first contact printf function. With this function, you can print an indefinite number of variables to the screen, such as:
printf ("%d", 3);
printf ("%d,%d", 3,4);
The above code seems simple, but in fact we need to solve many problems. When we were designing printf, we didn't know how many parameters were going to be passed in. In this unknown situation, we need to address the following issues:
How do we tell printf we're going to pass in some parameters
How does printf access these parameters
How the system frees the parameters from the delivery stack when the function call is complete
To solve these problems, we first want to explain the cdecl calling convention, where all functions using indefinite parameters must be calling conventions using CDECL (global function) or this call (class member function). This convention provides for parameter passing as follows:
Parameters from right to left into the stack (that is, if you call F (a,b,c), C first into the stack, then B, and finally a into the stack)
The caller is responsible for cleaning up the stack
The 2nd directly solves the third problem in the previous three questions. Let's say two other questions in detail.
Determine the number of parameters
In a function, you typically have the following Prelog code:
00401020 Push EBP
00401021 mov Ebp,esp
00401023 Sub esp,48h
After the above code is executed, the stack context in which the Func (a,b,c) function functions becomes the following layout: