a_fischetti
May 5th, 1999, 08:19 AM
Is it possible to create a C or C++ function with a variable number of parameters?
Many thanks for your help.
Antonio.
_______
Many thanks for your help.
Antonio.
_______
|
Click to See Complete Forum and Search --> : void func(p1,p2,p3,...?) a_fischetti May 5th, 1999, 08:19 AM Is it possible to create a C or C++ function with a variable number of parameters? Many thanks for your help. Antonio. _______ Franky Braem May 5th, 1999, 08:34 AM Yes you can. This is an example I found in the VC++ Help : int average(int first, ...) { int count = 0; int sum = 0; int i = first; va_list marker; va_start(marker, first); while(i != -1) { sum += i; count++; i = va_arg(marker, int); } return (sum ? (sum / count) : 0 ); } Other examples are the c-routines printf, sprintf, ... Franky Braem May 5th, 1999, 08:35 AM Yes you can. This is an example I found in the VC++ Help : int average(int first, ...) { int count = 0; int sum = 0; int i = first; va_list marker; va_start(marker, first); while(i != -1) { sum += i; count++; i = va_arg(marker, int); } va_end(marker); return (sum ? (sum / count) : 0 ); } Other examples are the c-routines printf, sprintf, ... Paul McKenzie May 5th, 1999, 10:22 AM Yes. Most C or C++ textbooks explains this technique. Alternately, do a search on "varargs.h" to get a rundown on how this is done. Regards, Paul McKenzie Paul McKenzie May 5th, 1999, 10:26 AM You're forgetting the most important part: the header: #include <varargs.h> for UNIX #include <stdio.h> #include <stdarg.h> for ANSI Regards, Paul McKenzie codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |