I'm trying to do something like a dictionary for c. I want that different types of data to be stored in a dictionary. In order to get an unlimited number of values, I used stdarg.h but got a problem with determining the data type. I have a few questions:
How can I determine the data type of each element?
How can I create a loop that will work until the elements are over?
For example this code from K&R
include <stdarg.h>
/* minprintf: минимальный printf с переменным числом аргумент */
void minprintf(char *fmt, ...)
{
va_list ар; /* указывает на очередной безымянный аргумент */
char *p, *sval;
int ival;
double dval;
va_start(ap, fmt); /* устанавливает ар на 1-й безымянный аргумент */
for (p = fmt; *р; р++) {
if (*p != '%') {
putchar(*p);
continue;
}
switch (*++р) {
case 'd':
ival = va_arg(ap, int);
printf ("%d", ival);
break;
case 'f':
dval = va_arg(ap, double);
printf("%f", dval);
break;
case 's':
for (sval = va_arg(ap, char *); *sval; sval++)
putchar(*sval);
break;
default:
putchar(*p);
break;
}
}
va_end(ap); /* очистка, когда все сделано */
}
And here fmt is string that contains quantity elements.
But how can I write a function that doesn't contains this string?
For example:
#include <stdio.h>
#include <stdarg.h>
void test (const char *, ...);
int main ()
{
test("Hello", "world", 15, 16.000);
return 0;
}
Thanks a lot for taking your time.