[comp.sys.amiga.tech] Variable Argument Lists in Lattice C 4.1

dmg@ssc-vax.UUCP (David Geary) (08/23/88)

In article <296@nfsun.UUCP>, Kurt Geisel writes:

>Is there any way to get Lattice C 4.1 to provide variable argument
>list support using the standard varargs.h/stdarg.h macros?

Well, I don't know about Lattice C 4.1 (I still have 3.03, and am
proud of it ;-), but you can implement variadic functions (variable
number of arguments of variable types) yourself.

Here's a little example of implementing variadic functions.  I've
compiled and ran this on Suns, Apollos, Amiga and even (gasp!)
Vaxes, so I'm convinced it is *portable*.

Actually, there is no voodoo involved in varargs.h in Unix.  You can
look at /usr/include/varargs.h on a Unix system, and see for
yourself.  It's just a matter of a little appropriate casting.
Anyway, here's the source:

#define VarArg(argp,type) ( (type *) (argp += sizeof(type)) )[-1]

#define END_OF_ARGS 0

#define INT     1
#define FLOAT   2
#define STRING  3
#define SHORT   4

AcceptVarArgs(VargList)
  int  VargList;
{
  char  *NextArg = (char *)&VargList;
  int    NextArgType;

  while((NextArgType = VarArg(NextArg,int))  != END_OF_ARGS)
  {
    switch(NextArgType)
    {
      case INT:     
        printf("You Sent INT:  %d\n", VarArg(NextArg,int));
	break;
      
      case FLOAT:   
        printf("You Sent FLOAT:    %f\n", (float )VarArg(NextArg,double));
                    break;

      case STRING:  
        printf("You Sent STRING:   %s\n", VarArg(NextArg,char *));
        break;

      case SHORT:   
        printf("You Sent SHORT:    %d\n", (short )VarArg(NextArg,int));
	break;
    }
  }

puts("\n\n");
}

main()
{
  static char string[] = "Here's the string";
  int   x=5;
  float f=5.25;
  short s=10;

  AcceptVarArgs(INT,x,STRING,string,END_OF_ARGS); 
  AcceptVarArgs(INT,x,FLOAT,f,STRING,string,END_OF_ARGS);
  AcceptVarArgs(STRING,string,FLOAT,f,INT,x,END_OF_ARGS);
  AcceptVarArgs(STRING,string,FLOAT,f,INT,x,STRING,string,END_OF_ARGS);
}


  I never did like the way Unix does their varargs stuff - I always
forget and put in a semicolon.  Anyway, if you look at this stuff and
figure out how it works, you can always rewrite the macros yourself,
and you don't need varargs.h, or any other such tomfoolry.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ David Geary, Boeing Aerospace,               ~ 
~ Seattle - "THE DRIZZLE CAPITAL OF THE WORLD" ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~