Three point __ function in C function parameter

Source: Internet
Author: User

Original link One: http://hi.baidu.com/wjun520/blog/item/1678a11da07fe68086d6b653.html

C + + has a function overload this method, so that we can call to be uncertain of the number of arguments, in fact, the C language can also be more sophisticated.

We can see the prototype of the printf () function in stdio.h:

int printf (char * format,...)

In fact, if we want to write such a function can also be similar to write, then define the function with this symbol "... ", it is called a placeholder, shout it" three point "also can, as long as you want. So I can define my functions like this:

Fun (int a,...)

{   }

As long as the class listened carefully to the classmate (except for fools) know that this is an empty function, it is nothing to do, the light is not enough to write, specific should how to define it.

And let me introduce you to 3 small stuff:

1, Va_list

2, Va_arg ()

3, Va_start ()

In the study of these 3 small dongdong, we first recall how the C language is how to operate the file, how to deal with the data in memory. When learning the file operation, we mentioned the concept of "flow", we use the pointer to the memory address where the data resides, and then one operation.

When learning pointers, we know that there is a function pointer, not a pointer function but a function injection oh. (Oh, my classmate if remember to review, don't think I long-winded ^_^). We remember that when the program is executing, the function is stored in memory. Now in depth, a little bit, when storing functions, the process of parameter passing is how to achieve it. What are the so-called formal parameters (local variables) in essence? Think about these problems, come to think of it, your mind is on the roll.

When you call a function, the program also passes the arguments in, saves them in the function store, and saves them together if you have many parameters.

It's going to take va_list, it's a type definition, we can think of it as a pointer to the address of the first parameter.

If we are so defined: Va_list pp;

Then PP is a variable that points to the first argument in all parameters. It is different from the general pointer variable, it is a composite variable, what is a composite variable AH. Type of structure, hehe. If a is the first parameter, can you write pp=a?

Suppose I define char d[]= "Ruixin", e[]= "Gelin"; I'm going to assign the value of E to D, can I write d=e? Have to use strcpy (), right. Oh, the same truth, here we also use a function to achieve, it is va_start ();

If so write: Va_start (pp,a);

Then the PP points to the first parameter A and gets the type int of a.

At this point, if you have the next argument, you need to have the PP refer to a parameter down and get its type. Also need to use the function to implement, this function is: Va_arg ()

It can be written like this: Va_arg (PP, type) so that the PP points to a parameter and can get the type of that parameter.

Attention. The type is very important, learn the pointer should be clear, the type of the pointer if wrong, the correct location, the number can be taken out of the mess.

Now let's look at a simple example:

#include <stdio.h>

#include <stdarg.h>

void Fun (int a,...)

{

Va_list pp;

int n=1;//use N to measure number of parameters

Va_start (Pp,a);

Todo

{

printf ("%d parameter =%d/n", n++,s);

A=va_arg (pp,int);//Make PP refer to the next parameter, assign the value of the following parameter to the variable a

}

while (a!=0);//until the parameter is 0 o'clock to stop the loop

}

Main ()

{

Fun (20,40,60,80,0);

}

Attention.

Be sure to have the above two files contain the command, because the program used the 3 little things are in that file. In fact, the real meaning should say that is a function, in essence that is only two macro, hehe.

What is a macro, what is a function, not what is here to say, and this is not too much relationship. Write it, just answer a question why ...

Original link two: http://blog.csdn.net/joliny/archive/2008/04/28/2340299.aspx

Va_list is a group of macros that solve the problem of variable parameters in C language.

Usage of va_list:
(1) First in the function defines a va_list type of variable, which is a pointer to the parameter
(2) Then use the Va_start macro to initialize the variable just defined by the va_list variable, the second parameter of the macro is the first parameter of the variable parameter, which is a fixed parameter.
(3) Then return the variable parameter with Va_arg, and the second parameter of Va_arg is the type of the parameter you want to return.
(4) Finally using the Va_end macro to end the acquisition of variable parameters. Then you can use the second argument in the function. If the function has more than one variable parameter, call Va_arg in turn to get each parameter.

Va_list processing in the compiler:
1 after running Va_start (AP,V), the AP points to the address of the first variable parameter on the stack.
(2) Va_arg () Gets the variable parameter value of type T, first apt = sizeof (t type) in this step, let the AP refer to the address of the next parameter. It then returns the T-type * pointer of the ap-sizeof (t type), which is the address of the first variable parameter on the stack. Then use * to get the content of this address.
(3) Va_end (), X86 platform defined as AP = ((char*) 0), so that the AP no longer points to the stack, but as NULL, some directly defined ((void*) 0), so that the compiler does not generate code for Va_end, For example, GCC is defined in the Linux X86 platform.

Note that the parameter cannot be declared as a register variable, or as a function or array type, because the address of the parameter is used to Va_start macros.

Issues to be noted with va_list:
(1) Because Va_start, va_arg, va_end definition macro, so it seems very stupid, the type and number of variable parameters completely in the function by the program code control, it can not intelligently identify the number and type of different parameters. In other words, you want to implement intelligent recognition of variable parameters is to be done in your own program to make judgments.
(2) There is another problem, because the compiler for variable parameters of the function of the prototype check is not strict enough for error-checking programming. It is not good for us to write high quality code.


Summary: The function principle of variable parameters is actually very simple, and the VA series is defined as a macro definition, and the implementation is related to the stack. We write a variable function of the C function, there are advantages and disadvantages, so in unnecessary situations, we do not need to use variable parameters, if in C + +, we should use C + + polymorphism to achieve variable parameters of the function, as far as possible to avoid using the C language method to achieve.

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.