C Questions

John Hascall hascall at atanasoff.cs.iastate.edu
Wed Oct 25 04:40:21 AEST 1989


In article <1248 at utkcs2.cs.utk.edu> wozniak at utkux1.utk.edu (Bryon Lape) writes:
 
}	I have 2 questions concerning function definitions:  How does
}one write a function so that some, not all, or the items passed can be
}any type of variable at any time?  How does one write a function so that
}any number of variables can be sent?

      Use Ada?  :-)
      Use varargs.
 
}	Any example of the first question is a generic swap().  I know
}that pointers to void type are used in some way, but I do not know how.
}An example of the second is the printf() where the passed string will
}determine the other passed variables.  This one is most likely taken
}care of at compile time, but I am talking run time.

     Printf's control string is generally evaluated at run time.

     I suppose you could use that or similar techniques, i.e.:

	 void swap(void *x, void *y, size_t bytes) {

	 tmp = malloc(bytes);      /* just a program sketch.... no flames */
	 memcpy(tmp,x, bytes);     /* on it's lack of completeness PLEASE */
	 memcpy(x, y, bytes);
	 memcpy(y, tmp, bytes);
	 free(tmp);
	 }

	 swap(&foo1, &foo2, sizeof(struct foo));

John Hascall



More information about the Comp.lang.c mailing list