📄 varargs1.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN"><!-- This collection of hypertext pages is Copyright 1995-2005 by Steve Summit. --><!-- Content from the book "C Programming FAQs: Frequently Asked Questions" --><!-- (Addison-Wesley, 1995, ISBN 0-201-84519-9) is made available here by --><!-- permission of the author and the publisher as a service to the community. --><!-- It is intended to complement the use of the published text --><!-- and is protected by international copyright laws. --><!-- The on-line content may be accessed freely for personal use --><!-- but may not be published or retransmitted without explicit permission. --><!-- --><!-- this page built Sat Dec 24 21:47:46 2005 by faqproc version 2.7 --><!-- from source file varargs.sgml dated Sun Nov 25 08:09:42 2001 --><!-- corresponding to FAQ list version 4.0 --><html><!-- Mirrored from c-faq.com/varargs/varargs1.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:00 GMT --><head><meta name=GENERATOR content="faqproc"><title>Question 15.4</title><link href="proto2.html" rev=precedes><link href="vprintf.html" rel=precedes><link href="index.html" rev=subdocument></head><body bgcolor="#ffffff"><a href="proto2.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="vprintf.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../index-2.html"><img src="../images/buttontop.gif" alt="top/contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><hr><p><!-- qbegin --><h1>comp.lang.c FAQ list<font color=blue>·</font><!-- qtag -->Question 15.4</h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I write a function that takes a variable number ofarguments?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Usethe facilities ofthe <TT><stdarg.h></TT>header.</p><p>Here is a function which concatenates an arbitrary number ofstrings into <TT>malloc</TT>'ed memory:</p><pre>#include <stdlib.h> /* for malloc, NULL, size_t */#include <stdarg.h> /* for va_ stuff */#include <string.h> /* for strcat et al. */char *vstrcat(const char *first, ...){ size_t len; char *retbuf; va_list argp; char *p; if(first == NULL) return NULL; len = strlen(first); va_start(argp, first); while((p = va_arg(argp, char *)) != NULL) len += strlen(p); va_end(argp); retbuf = malloc(len + 1); /* +1 for trailing \0 */ if(retbuf == NULL) return NULL; /* error */ (void)strcpy(retbuf, first); va_start(argp, first); /* restart; 2nd scan */ while((p = va_arg(argp, char *)) != NULL) (void)strcat(retbuf, p); va_end(argp); return retbuf;}</pre>(Note thata second call to <TT>va_start</TT> is neededto re-start the scanwhen the argument list is processed a second time.Note the calls to <TT>va_end</TT>:they're important for portability,even if they don't seem to do anything.)<p>A call to <TT>vstrcat</TT> looks something like<pre> char *str = vstrcat("Hello, ", "world!", (char *)NULL);</pre>Note the cast on the last argument;see questions <a href="../null/null2.html">5.2</a> and <a href="proto2.html">15.3</a>.(Also note that the caller must free the returned,<TT>malloc</TT>'ed storage.)</p><p><TT>vstrcat</TT> acceptsa variable number of arguments,all of type <TT>char *</TT>.Here is an example which accepts a variable number of arguments of different types;it is a stripped-down versionof the familiar <TT>printf</TT> function.Note that each invocation of <TT>va_arg()</TT> specifies the type of the argumentbeingretrievedfrom the argument list.</p><p>(The <TT>miniprintf</TT> function here uses<TT>baseconv</TT>from question <a href="../misc/hexio.html">20.10</a> to formatnumbers.Itissignificantlyimperfectin that it will not usually be able to printthe smallest integer, <TT>INT_MIN</TT>, properly.)<pre>#include <stdio.h>#include <stdarg.h>#ifdef MAINvoid miniprintf(const char *, ...);main(){ miniprintf("Hello, world!\n"); miniprintf("%c %d %s\n", '1', 2, "three"); miniprintf("%o %d %x\n", 10, 10, 10); miniprintf("%u\n", 0xffff); return 0;}#endifextern char *baseconv(unsigned int, int);voidminiprintf(const char *fmt, ...){ const char *p; int i; unsigned u; char *s; va_list argp; va_start(argp, fmt); for(p = fmt; *p != '\0'; p++) { if(*p != '%') { putchar(*p); continue; } switch(*++p) { case 'c': i = va_arg(argp, int); /* *not* va_arg(argp, char); see Q <a href="float.html">15.10</a> */ putchar(i); break; case 'd': i = va_arg(argp, int); if(i < 0) { /* XXX won't handle INT_MIN */ i = -i; putchar('-'); } fputs(baseconv(i, 10), stdout); break; case 'o': u = va_arg(argp, unsigned int); fputs(baseconv(u, 8), stdout); break; case 's': s = va_arg(argp, char *); fputs(s, stdout); break; case 'u': u = va_arg(argp, unsigned int); fputs(baseconv(u, 10), stdout); break; case 'x': u = va_arg(argp, unsigned int); fputs(baseconv(u, 16), stdout); break; case '%': putchar('%'); break; } } va_end(argp);}</pre></p><p>See also question <a href="oldvarargs.html">15.7</a>.</p><p>References:K&R2 Sec. 7.3 p. 155, Sec. B7 p. 254<br>ISO Sec. 7.8<br>Rationale Sec. 4.8<br>H&S Sec. 11.4 pp. 296-9<br>CT&P Sec. A.3 pp. 139-141<br>PCS Sec. 11 pp. 184-5, Sec. 13 p. 242<br></p><!-- aend --><p><hr><a href="proto2.html" rev=precedes><img src="../images/buttonleft.gif" alt="prev"></a><a href="index.html" rev=subdocument><img src="../images/buttonup.gif" alt="up"></a><a href="vprintf.html" rel=precedes><img src="../images/buttonright.gif" alt="next"></a> <a href="../questions.html"><img src="../images/buttontop.gif" alt="contents"></a><a href="../search.html"><img src="../images/buttonsrch.gif" alt="search"></a><br><!-- lastfooter --><a href="../about.html">about this FAQ list</a> <a href="../eskimo.html">about eskimo</a> <a href="../search.html">search</a> <a href="../feedback.html">feedback</a> <a href="copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/varargs/varargs1.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:00 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -