C language Variable parameter list application-multi-string copying and linking

Source: Internet
Author: User

The <stdarg.h> header file in the C standard library contains the implementation of the variable parameter list, completing the function of traversing the unknown number and type of function parameter lists. Provides the following 3 macros and va_list variables:

Va_start (va_list AP, Lastarg): This macro must be invoked to initialize the variable parameters before extracting them.

Va_arg (va_list AP, Type_of_var): For extracting variables, Type_of_var is the type of the extracted variable. Returns the parameters of the corresponding type.

Va_end (Va_list AP): After the parameters have been processed, you must call Va_end to do some cleanup.

With these 3 macros, you can iterate through the variable parameters, and look at the multiple-character replication and link implementations, both of which pass NULL as the end of the variable argument list.

void Nstrcat (char *dest, ...) {
    va_list     ap; 
    Char        *p, *q; 

    Va_start (AP, dest);
    while (p = Va_arg (AP, char *))!= NULL) {
        strcat (dest, p); 
        Dest + = strlen (p);
    }   
        
    Va_end (AP);
}

void nstrcpy (char *dest, ...) {
    va_list     ap; 
    Char        *p; 

    Va_start (AP, dest);
    while (p = Va_arg (AP, char *))!= NULL) {
        strcpy (dest, p);
        Dest + = strlen (p);
    }

    Va_end (AP);
}
Run the test:

int
Main (int argc, char** argv) {
    char    buf[1000], bufa[100];
    Char    a[] = "abc,123";
    Char    b[] = "456";
    Char    c[] = "090";

    nstrcpy (buf, A, B, C, NULL);

    strcpy (Bufa, "nbakk__");
    Nstrcat (Bufa, B, "__", A, "__", C, NULL);

    printf ("nstrcpy:%s\n", buf);
    printf ("Nstrcat:%s\n", Bufa);
}
Through these two functions can be very convenient to implement the operation of multiple strings, the output of the program is:

nstrcpy:abc,123456090
nstrcat:nbakk__456__abc,123__090
Let's look at the implementation of the variable parameter (Variable Arguments), such as we call a function that passes in multiple int variable parameters, such as avg (int a, ...).

|--------------|

|       23 | Last variable parameter

|--------------| 0x10000008

|       12 | First variable parameter

|--------------| 0x10000004

|       1 | Last fixed parameter

%ebp--> |--------------| 0x10000000

| |

|--------------|

............

|--------------|

| |

%esp--> |---------------|

The CPU%EBP is the frame pointer,%ESP is the stack pointer, the memory between%EBP and%esp is the stack frame of the current function, and the arguments passed to the current function are in the stack frame of the calling function, in the order placed behind the%EBP. For example, the graph above is the state of the stack corresponding to the call AVG (1, 12, 23). According to the state of the stack, a corresponds to the 1,a's starting address is the 0x10000000,va_list variable is actually char *, when the call Va_list (AP, a), the AP corresponds to 0x10000004. When the call int n = va_arg (AP, int), which is equivalent to executing (* (int *) AP) is 12, and then the AP = sizeof (int), then the AP is pointing to 0x10000008, which is the type type of the incoming va_arg, skipping sizeof (type) byte, so that the variable parameters are traversed. This is my understanding of the implementation of variable parameters and guess, not necessarily ah ...


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.