Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!dino!atanasoff!hascall From: hascall@atanasoff.cs.iastate.edu (John Hascall) Newsgroups: comp.lang.c Subject: Re: C Questions Message-ID: <1668@atanasoff.cs.iastate.edu> Date: 24 Oct 89 18:40:21 GMT References: <1248@utkcs2.cs.utk.edu> Reply-To: hascall@atanasoff.UUCP (John Hascall) Organization: Iowa State Univ. Computation Center Lines: 32 In article <1248@utkcs2.cs.utk.edu> wozniak@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