One-step write algorithm (variable parameters)

Source: Internet
Author: User

 

[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 );

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.