Varargs.h for Mac LSC
Joe Giannuzzi
joseph at cooper.cooper.EDU
Fri Feb 24 07:50:12 AEST 1989
Varargs is a portable method of passing multiple parameters to
a function. It is available on most **IX based systems. It is
not part of the #include files for LSC on the Mac, so when I had
the need to port a **IX based program to my Mac I had to create
the varargs.h #include file. The very short listing follows.
------------------------------------------------------------------
/* Varargs.h
a portable method of passing multiple parameters to a function
JWG 2/18/89 */
#define va_dcl va_list va_alist;
#define va_arg(A,B) *(B*)((A+=sizeof(B))-sizeof(B))
#define va_start(A) A=(char *)(&va_alist)
#define va_end(A) /* va_end */
typedef char *va_list;
------------------------------------------------------------------
The function being called is responsible for knowing what
parameter to get from the stack. This is usually done by
passing a format string. Note that char becomes int and
float becomes double when passed. Following is an example.
------------------------------------------------------------------
/* All "va_" variable and macro names cannot be changed. */
function(va_list)
va_dcl
{
va_list argp;
char *fmt;
char cval,*sval
long ival;
double fval;
va_start(argp);
fmt=va_arg(argp,char *);
while(*fmt!=NULL)
switch(*fmt++)
{
case READINT:
ival=(long)va_arg(argp,int);
break;
case READLONG:
ival=(long)va_arg(argp,long);
break;
case READCHAR: /* char passed as int */
cval=(char)va_arg(argp,int);
break;
case READSTR:
sval=va_arg(argp,char *);
break;
case READFLT: /* float passed as double */
fval=(float)va_arg(argp,double);
break;
case READDBL:
fval=(double)va_arg(argp,double);
break;
default:
/* ERROR IN FORMAT STRING */
break;
}
va_end(argp);
}
------------------------------------------------------------------
Enjoy. Joe Gunoz, joseph @cooper.cooper.edu
More information about the Comp.lang.c
mailing list