Page 1 of 1
Ellipsis Operator
Posted: 16 Jun 2008 00:50
by Nick101
Hello,
Can someone explain how to use Ellipsis ( ... ) operator? I found a very short explanation in the help file which is I can not understand how to use it to create a variable number argument function. Here is what I found in the help file :
Ellipsis ('...') Operator
An ellipsis ('...') consists of three successive periods with no whitespace intervening. You can use an ellipsis in the formal argument lists of function prototypes to indicate a variable number of arguments, or arguments with varying types. For example:
void func (int n, char ch, ...);
This declaration indicates that func will be defined in such a way that calls must have at least two arguments, an int and a char, but can also have any number of additional arguments.
Example:
#include <stdarg.h>
int addvararg(char a1,...){
va_list ap;
char temp;
va_start(ap,a1);
while( temp = va_arg(ap,char))
a1 += temp;
return a1;
}
int res;
void main() {
res = addvararg(1,2,3,4,5,0);
res = addvararg(1,2,3,4,5,6,7,8,9,10,0);
}//~!
Re: Ellipsis Operator
Posted: 29 May 2020 08:45
by design4
Nick101 wrote: ↑16 Jun 2008 00:50
Hello,
Can someone explain how to use Ellipsis ( ... ) operator? I found a very short explanation in the help file which is I can not understand how to use it to create a variable number argument function. Here is what I found in the help file :
Ellipsis ('...') Operator
An ellipsis ('...') consists of three successive periods with no whitespace intervening. You can use an ellipsis in the formal argument lists of function prototypes to indicate a variable number of arguments, or arguments with varying types. For example:
void func (int n, char ch, ...);
This declaration indicates that func will be defined in such a way that calls must have at least two arguments, an int and a char, but can also have any number of additional arguments.
Example:
#include <stdarg.h>
int addvararg(char a1,...){
va_list ap;
char temp;
va_start(ap,a1);
while( temp = va_arg(ap,char))
a1 += temp;
return a1;
}
int res;
void main() {
res = addvararg(1,2,3,4,5,0);
res = addvararg(1,2,3,4,5,6,7,8,9,10,0);
}//~!
Hello
Im also looking for the example because there is no vsnprintf in the stdarg. I want to build a string with some other int variable inside it
Regret
Re: Ellipsis Operator
Posted: 02 Jun 2020 12:26
by filip
Hi,
Maybe the following example will help :
Code: Select all
// using ellipsis to get the average value of the variable number of parameters
#include <stdarg.h>
// average result value is placed here
double result;
// The ellipsis must be the last parameter
// count is how many additional arguments we're passing
double findAverage(int count, ...) {
// sum value is placed here
double sum = 0;
// counter variable
int i = 0;
// We access the ellipsis through a va_list, so let's declare one
va_list list;
// We initialize the va_list using va_start.
// The first parameter is the list to initialize.
// The second parameter is the last non-ellipsis parameter.
va_start(list, count);
// Loop through all the ellipsis arguments
for (i=0; i < count; ++i)
// We use va_arg to get parameters out of our ellipsis
// The first parameter is the va_list we're using
// The second parameter is the type of the parameter
sum += va_arg(list, int);
return sum / count;
}
void main() {
result = findAverage(5, 1, 2, 3, 4, 5);
result = findAverage(6, 1, 2, 3, 4, 5, 6);
}
Basically, we are doing the following here :
- Define a function with its last parameter as ellipses (i.e. ...) and the one just before will represent the number of arguments.
- Create a va_list type variable in the function definition (this type is defined in stdarg.h header file).
- Use the first parameter and va_start macro to initialize the va_list variable to an argument list (the macro va_start is defined in stdarg.h header file).
- Use va_arg macro and va_list variable to access each item in argument list.
Regards,
Filip.
Re: Ellipsis Operator
Posted: 03 Jun 2020 01:39
by design4
filip wrote: ↑02 Jun 2020 12:26
Hi,
Maybe the following example will help :
Code: Select all
// using ellipsis to get the average value of the variable number of parameters
#include <stdarg.h>
// average result value is placed here
double result;
// The ellipsis must be the last parameter
// count is how many additional arguments we're passing
double findAverage(int count, ...) {
// sum value is placed here
double sum = 0;
// counter variable
int i = 0;
// We access the ellipsis through a va_list, so let's declare one
va_list list;
// We initialize the va_list using va_start.
// The first parameter is the list to initialize.
// The second parameter is the last non-ellipsis parameter.
va_start(list, count);
// Loop through all the ellipsis arguments
for (i=0; i < count; ++i)
// We use va_arg to get parameters out of our ellipsis
// The first parameter is the va_list we're using
// The second parameter is the type of the parameter
sum += va_arg(list, int);
return sum / count;
}
void main() {
result = findAverage(5, 1, 2, 3, 4, 5);
result = findAverage(6, 1, 2, 3, 4, 5, 6);
}
Basically, we are doing the following here :
- Define a function with its last parameter as ellipses (i.e. ...) and the one just before will represent the number of arguments.
- Create a va_list type variable in the function definition (this type is defined in stdarg.h header file).
- Use the first parameter and va_start macro to initialize the va_list variable to an argument list (the macro va_start is defined in stdarg.h header file).
- Use va_arg macro and va_list variable to access each item in argument list.
Regards,
Filip.
Thank you. But can u tell me how to solve below problem?
Code: Select all
main()
{
char IDE[7] = "MikroC";
int version = 6;
MyUART1Print("Hello, Im using %s version %d", IDE, version);
}
//Then here is the function code with 1 buffer to be transmit using "UART1_Write_Text(buffer)";
Can u help me use (...) ? Because I want to combine other variable in one buffer. I cannot simply write "UART1_Write_Text("Hello, Im using %s version %d", IDE, version)".
Thanks in advanced