[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Variable parameters are a feature of C programming. In our general programming, the number of function parameters is determined in advance. However, for some functions, the number and length of the functions are not certain. Is there any secret in the middle?
In fact, we can recall which functions are variable parameters? It is actually a function like sprintf and printf. So what are the rules of these functions? The key is above this string. Let's take an example,
Void test ()
{
Printf ("% s, value = % d \ n", "hello", 10 );
}
Void test ()
{
Printf ("% s, value = % d \ n", "hello", 10 );
}
The test function is also a simple printing function. So what's special about this function? % s, % d, and the following characters correspond one by one. So how many such characters are there, the number of parameters left after the first parameter. How can I obtain the address of the following parameter? We can recall how the stack is generally processed by the pressure on the stack,
/*
* Stack space:
*
* Parameter 3 | up
* Parameter 2 |
* Parameter 1 v down
*/
/*
* Stack space:
*
* Parameter 3 | up
* Parameter 2 |
* Parameter 1 v down
*/Because the parameters are pushed from right to left, the address of the following parameters can be processed according to "%" in sequence. Next, we can write a PrintInt function to print int data. First, create a framework,
Void PrintInt (char * buffer, int data ,...)
{
Return;
}
Void PrintInt (char * buffer, int data ,...)
{
Return;
} Then verify whether the buffer parameter contains % d. If such a character exists, an integer must be printed,
Void PrintInt (char * buffer, int data ,...)
{
Static char space [1024];
Char temp [32];
Int * start;
Int count;
If (NULL = buffer)
Return;
Memset (space, 0, 1024 );
Memset (temp, 0, 32 );
Start = (int *) & buffer;
Count = 0;
While (buffer [count]) {
If (! Strncmp (& buffer [count], "% d", strlen ("% d "))){
Start ++;
Itoa (* start, temp, 10 );
Strcat (space, temp );
Count + = 2;
Continue;
}
Space [strlen (space)] = buffer [count];
Count ++;
}
Memset (buffer, 0, strlen (buffer ));
Memmove (buffer, space, strlen (space ));
Return;
}
Void PrintInt (char * buffer, int data ,...)
{
Static char space [1024];
Char temp [32];
Int * start;
Int count;
If (NULL = buffer)
Return;
Memset (space, 0, 1024 );
Memset (temp, 0, 32 );
Start = (int *) & buffer;
Count = 0;
While (buffer [count]) {
If (! Strncmp (& buffer [count], "% d", strlen ("% d "))){
Start ++;
Itoa (* start, temp, 10 );
Strcat (space, temp );
Count + = 2;
Continue;
}
Space [strlen (space)] = buffer [count];
Count ++;
}
Memset (buffer, 0, strlen (buffer ));
Memmove (buffer, space, strlen (space ));
Return;
} To verify whether our function is correct, you can compile a test function to verify it,
Void display ()
{
Char buffer [32] = {"% d \ n "};
PrintInt (buffer, 1, 2, 3, 4 );
Printf (buffer );
}
Void display ()
{
Char buffer [32] = {"% d \ n "};
PrintInt (buffer, 1, 2, 3, 4 );
Printf (buffer );
}