|
-
May 5th, 1999, 08:19 AM
#1
void func(p1,p2,p3,...?)
Is it possible to create a C or C++ function with a variable number of parameters?
Many thanks for your help.
Antonio.
_______
-
May 5th, 1999, 08:34 AM
#2
Re: void func(p1,p2,p3,...?)
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, ...
-
May 5th, 1999, 08:35 AM
#3
Re: void func(p1,p2,p3,...?)
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, ...
-
May 5th, 1999, 10:22 AM
#4
Re: void func(p1,p2,p3,...?)
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
-
May 5th, 1999, 10:26 AM
#5
Re: void func(p1,p2,p3,...?)
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|