|
|
1.1 ! root 1: ! 2: ! 3: variable arguments Overview variable arguments ! 4: ! 5: ! 6: ! 7: ! 8: The ANSI Standard mandates the creation of a set of routines to ! 9: help implement functions, such as pprriinnttff and ssccaannff, that take a ! 10: variable number of arguments. These routines are called from ! 11: within another function to help it handle its arguments. ! 12: ! 13: These routines are declared or defined in the header ssttddaarrgg.hh, ! 14: and are as follows: ! 15: ! 16: ! 17: vvaa_aarrgg() Return pointer to next argument in argument list ! 18: vvaa_eenndd() Tidy up after an argument list has been traversed ! 19: vvaa_ssttaarrtt()Initialize object that holds function arguments ! 20: ! 21: ! 22: vvaa_aarrgg and vvaa_ssttaarrtt must be implemented as macros; vvaa_eenndd must be ! 23: implemented as a library function. All three use the special ! 24: type vvaa_lliisstt, which is an object that holds the arguments to the ! 25: function being implemented. ! 26: ! 27: ***** Example ***** ! 28: ! 29: The following example concatenates multiple strings into a common ! 30: allocated string and returns the string's address. ! 31: ! 32: ! 33: #include <stdarg.h> ! 34: #include <stdlib.h> ! 35: #include <stddef.h> ! 36: #include <stdio.h> ! 37: ! 38: ! 39: ! 40: char * ! 41: multcat(numargs) ! 42: int numargs; ! 43: { ! 44: va_list argptr; ! 45: char *result; ! 46: int i, siz; ! 47: ! 48: ! 49: ! 50: /* get size required */ ! 51: va_start(argptr, numargs); ! 52: for(siz = i = 0; i < numargs; i++) ! 53: siz += strlen(va_arg(argptr, char *)); ! 54: ! 55: ! 56: ! 57: if ((result = calloc(siz + 1, 1)) == NULL) { ! 58: fprintf(stderr, "Out of space\n"); ! 59: exit(EXIT_FAILURE); ! 60: } ! 61: va_end(argptr); ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: variable arguments Overview variable arguments ! 70: ! 71: ! 72: ! 73: ! 74: ! 75: ! 76: va_start(argptr, numargs); ! 77: for(i = 0; i < numargs; i++) ! 78: strcat(result, va_arg(argptr, char *)); ! 79: va_end(argptr); ! 80: return(result); ! 81: } ! 82: ! 83: ! 84: ! 85: int ! 86: main(void) ! 87: { ! 88: printf(multcat(5, "One ", "two ", "three ", ! 89: "testing", ".\n")); ! 90: } ! 91: ! 92: ! 93: ***** See Also ***** ! 94: ! 95: lliibbrraarriieess, ssttddaarrgg.hh ! 96: ! 97: ! 98: ! 99: ! 100: ! 101: ! 102: ! 103: ! 104: ! 105: ! 106: ! 107: ! 108: ! 109: ! 110: ! 111: ! 112: ! 113: ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.