varargs functions: calling and prototyping

Mark Lawrence mark at DRD.Com
Thu Jul 26 05:59:44 AEST 1990


I'm using gcc 1.37 or so on a Sun SparcStation running 4.0.3.

I've got a function whose first three arguments should always be provided
but the rest of the arguments will be handed to vsprintf for formatting.

The function itself looks like:

#include <stdio.h>
#include <varargs.h>
#include <alarms.h> /* which includes a function prototype for this func */

#define AlarmBufSz 1024
static char     buf[AlarmBufSz];

/*VARARGS0*/
void
ReportAlarm(va_alist)
va_dcl
{
    va_list         args;
    AlarmType       alarm;
    char           *source;
    char           *fmt;

    va_start(args);
    source = va_arg(args, char *);
    alarm = va_arg(args, AlarmType);
    fmt = va_arg(args, char *);
    (void) vsprintf(buf, fmt, args);
    va_end(args);
	/* and stuff ... */
}

alarms.h looks like:

#ifndef ALARMS_H
#define ALARMS_H

typedef enum {
    alNone, alFoo,
    alLastAlarm
}               AlarmType;
/* void ReportAlarm(char * source, AlarmType alarm, char * fmt, ...) */
void ReportAlarm();
#endif              /* ALARMS_H */

My questions are:

1) do I need to provide a terminating NULL when calling this function
(I've seen it done int the Thom Plum notes on the Draft C Standard, but
I've tried it in Saber without and it *seems* to work)

2) how do I prototype it?  I tried the above commented out prototype and
when compiling function, gcc gripes that it doesn't match its prototype
and quits.



More information about the Comp.lang.c mailing list