Re-review of variable parameters

Source: Internet
Author: User

    if   Our program is written like this   int main (int argc,char *argv[])  {      cout<<argv[1]<<endl;     return 0; }        run   a.out   command line   ./a.out  string   Here  string  is what you want to print   if rewriting  cout<<argv[0]<<endl;   then    Print  ./a.out cout<<argv[2]<<endl;   so    nothing   of course    you can type in this  ./a.out   123  456 789  will print    456  variable parameter list   is actually doing a predefined area   dividing an area first to install the upcoming parameters   of course   the same type of     #include  <iostream>using  namespace std;int m (int argc,char *argv[])  {     cout <<&argv[0]<< ":" &LT;&LT;ARGV[0]&LT;&LT;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;COUT&LT;&LT;&AMP;ARGV [1]<< ":" <<argv[1]<<endl;     cout<<&argv[2]<< ":" <<argv[2]<<endl;      cout<<&argv[3]<< ":" <<argv[3]<<endl;      cout<<&argv[4]<< ":" <<argv[4]<<endl;        return 0; }   int main (int argc,char *argv[])// Exchange parameter Order    warning and cannot run  {    m (ARGC,ARGV);  return 0; }        [[email protected] ~]$ ./a.out can be used in addition to the   main function    common functions  111 222 333 444 555 666 7770xbfe7ca54:./a.out0xbfe7ca58:1110xbfe7ca5c : 2220xbfe7ca60:3330xbfe7ca64:444 can see     the string in the back of the first stack    high address  C  program stack at the bottom of the high address, the top of the stack is low address, So the above example can show that the function parameter in the stack order is really right-to-left   variadic function    can be implemented by function overloading  666  article  http://www.cnblogs.com/ dongsheng/p/4001555.html  But the test is not perfect &Nbsp;  inside the function vsprintf () compiler different   will not be #include <stdio.h> #include  <stdarg.h> int  mysum (int n,  ...) {    //  (1)   Define a parameter list     va_list ap;     //  (2)   Initialize parameter list     va_start (ap, n);    int  result = n;    int temp = 0;    //  Getting parameter values     while  ((Temp = va_arg (ap, int))  != end)      {        result += temp;     }    //  Close the parameter list     va_end (AP);    return  Result;} int main  () {    int m = mysum (1, 2, 3, 4, 5,  end);    printf  ("%d",  m); &NBSP;&NBSP;&NBSP;&NBSP;RETURN&Nbsp;0;}   Use macro definitions to handle the problem of   total need to end the symbol   otherwise   int m = mysum (1, 2, 3, 4,  5, -1,-1), so write   won't add -1  that value.


Re-review of variable parameters

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.