Args: var number & var types

carroll at snail.CS.UIUC.EDU carroll at snail.CS.UIUC.EDU
Fri May 13 03:01:00 AEST 1988


/* Written  3:43 pm  May 11, 1988 by jv0l+ at andrew.cmu.edu in snail:comp.lang.c */
/* ---------- "Args: var number & var types" ---------- */
Is there any way to declare a function to have two arguments point to the same
place in the stack-frame?  Let's say that I want to write the implementation of
foo which takes an optional argument based upon the first parameter:

{
  char c;
  int i;
  short s;
  long l;

  foo(0);     /* no optional argument */
  foo(1, c);  /* pass a character argument */
  foo(2, i);  /* pass an integer argument */
  foo(3, s);  /* pass a short arg */
  foo(4, l);  /* pass a long arg */
  foo(5, "Hello world"); /* pass a char* arg */
}
-Justin
justin.vallon at andrew.cmu.edu
/* End of text from snail:comp.lang.c */

union thing
    {
    char c;
    int i;
    short int s;
    long int l;
    char *q; /* in keeping with the 1 leter convention */
    } ;

void foo(type,object)
int type; union thing *object;
/* pointer, although many compilers allow pass-by-value union/structs */
    {
    switch(type)
	{
	case 0 : putchar(object->c); break;
	case 1 : printf("%d",object->i); break;
	...
	}
    }

main()
    {
    union thing fudge;
    foo(0,fudge.c='x',&fudge);
    foo(1,fudge.i=17,&fudge);
    ...
    /* this could be done with macro's, or better, just put the
       value into fudge instead of whatever other var, and then
       just call foo with foo(2,&fudge).
    */
    }

Alan M. Carroll		amc at woodshop.cs.uiuc.edu	carroll at s.cs.uiuc.edu
Grad Student (TA) / U of Ill - Urbana ...{ihnp4,convex}!uiucdcs!woodshop!amc
	"Too many fools who don't think twice
	 Too many ways to pay the price"  - AP & EW



More information about the Comp.lang.c mailing list