[C and pointer] indefinite Parameters

Source: Internet
Author: User

I. Variable Parameter instance [html] # include "stdio. h "/* function parameters are accessed in the form of Data Structure" stack ", from right to left. eg: */void fun (int ,...) {int * temp = & a; temp ++; int I; for (I = 0; I <a; ++ I) {printf ("% d \ n ", * temp); temp ++ ;}} int main () {int a = 1; int B = 2; int c = 3; int d = 4; fun (4, a, B, c, d); return 0;} 2. Use va_start to direct argp to the first optional parameter in va_list. Va_arg returns the current parameter in the parameter list and points argp to the next parameter in the parameter list. Va_end clears the argp pointer to NULL. The function can traverse these parameters multiple times, but all parameters must start with va_start and end with va_end [html # include <stdio. h> # include <string. h> # include <stdarg. h> int demo (char * msg ,...) {va_list argp; int argno = 0; char * para; va_start (argp, msg); while (1) {para = va_arg (argp, char *); if (strcmp (para, "") = 0) break; printf ("Parameter # % d is: % s \ n", argno, para); argno ++ ;} va_end (argp); return 0;} int main (void) {www.2cto.com demo ("DEMO ", "This", "is", "a", "demo! "," ");} 3. Use [html] # include <stdio. h> # include <string. h> # include <stdarg. h> void myprintf (char * fmt ,...) {char * pArg = NULL; char c; pArg = (char *) & fmt; pArg + = sizeof (fmt); do {// judge each character, until the end of the entire statement c = * fmt; if (c! = '%') Putchar (c); else {switch (* ++ fmt) {case 'D': printf ("% d", * (int *) pArg); break;} pArg + = sizeof (int);} + + fmt;} while (* fmt! = '\ 0'); pArg = NULL; // va_end} int main (void) {myprintf ("the fist: % d \ nthe secound: % d \ n ", 1, 2);} 4. For details, there is an stdarg in the include of VC ++ 6.0. the h header file has the following macro definitions: # define _ INTSIZEOF (n) (sizeof (n) + sizeof (int)-1 )&~ (Sizeof (int)-1) # define va_start (ap, v) (ap = (va_list) & v + _ INTSIZEOF (v )) // The first optional parameter address # define va_arg (ap, t) (* (t *) (ap + = _ INTSIZEOF (t)-_ INTSIZEOF (t ))) // next parameter address # define va_end (ap) (ap = (va_list) 0) // set the pointer to invalid [html] # include <iostream> # include <stdarg. h> const int N = 5; using namespace std; void Stdarg (int a1 ,...) {va_list argp; int I; int ary [N]; va_start (argp, a1); ary [0] = a1; for (I = 1; I <N; I ++) ary [I] = va_arg (argp, int); va_end (argp); for (I = 0; I <N; I ++) cout <ary [I] <endl;} void main () {Stdarg (5, 12, 64, 34, 23 );}

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.