I have implemented printf for vector type. The values in vector type are represented as (element1,element2,…) For example for vector of size 3, a possible value could be (1,2,3)
My implementation for printf is:
int my_printf(const char *format,...)
{
va_list args;
int argSize = 1; // get the number of elemts in vector
const char* vec;
vec = strchr(format,'v');
if(vec != NULL)
argSize = atol(vec + 1);
va_start (args, format);
int i = 0,ret = 0;
do // print all elements
{
ret = vprintf ("%d ", args);
fflush(stdout);
va_arg(args,float);
} while(i < argSize);
va_end (args);
return ret;
}
int main()
{
my_printf("v3",(10,12,13));
return 0;
}
While va_start (args, format); args gets value 13 prints it and for the next two printings prints 0 (args = 0)
What could be the solution?
(10,12,13)represent the single number 13. It’s comma operator, quite peculiar feature of C language.So your code is equivalent to this:
In C, there is no vector type as it’s known from C++. There are only arrays. You could initialize array like this:
–and use the array as an argument for
my_printf.