I'm having trouble collecting the values in a dynamic typing in C.
Values entered as char
, const char *
, int
, void *
, and long int
work perfectly when placed in printf
(prints the value without passing the union
field), but when I use data of type float
and double
the value returned is always 0
, only works if I pass the complete path of the variable of union
.
var.c:
#include <var.h>
const char *__var_type_2_string(var v){
switch (v.type){
case CHAR:
return "Char";
case INTEGER:
return "Int";
case L_INTEGER:
return "Long Int";
case FLOAT:
return "Float";
case DOUBLE:
return "Double";
case STRING:
return "String";
case POINTER:
return "Pointer";
}
return "Undefined";
}
var.h:
#ifndef __VAR_H__
#define __VAR_H__
#define typeof(var) _Generic( (var),\
char: CHAR,\
int: INTEGER,\
long int: L_INTEGER,\
char *: STRING,\
const char *: STRING,\
void *: POINTER,\
float: FLOAT,\
double: DOUBLE,\
default: 0)
#define set_wtype(var, out) _Generic( (var),\
char: (out.data.c = var),\
int: (out.data.i = var),\
long int: (out.data.l = var),\
char *: (out.data.s = var),\
const char *: (out.data.s = var),\
void *: (out.data.p = var),\
default: 0)
#define set_wtypef(var, out) _Generic( (var),\
float: (out.data.f = var),\
double: (out.data.d = var),\
default: 0)
enum VAR_TYPE{
CHAR = 1,
INTEGER = 2,
L_INTEGER = 3,
FLOAT = 4,
DOUBLE = 5,
STRING = 6,
POINTER = 7
};
typedef struct __attribute__((__packed__)) _var{
union _data{
char c;
int i;
long int l;
float f;
double d;
const char *s;
void *p;
} data;
enum VAR_TYPE type;
}var;
const char *__var_type_2_string(var v);
#define var_set(var, dta)\
var.type = typeof(dta);\
set_wtype(dta, var)
#define var_setf(var, dta)\
var.type = typeof(dta);\
set_wtypef(dta, var)
//#define var_get(var) var.data
#define var_type(var) var.type
#define var_typeS(var) __var_type_2_string(var)
#endif /*VAR_H*/
main.c:
#include <var.h>
#include <stdio.h>
int main(int argc, const char **argv){
/*Array*/
var vec[5];
var_set(vec[0], 37);
var_set(vec[1], 6625L);
var_set(vec[2], "Text");
var_set(vec[3], (char)'A');
var_setf(vec[4], 13.33f);
for(int x=0; x<5; x++){
puts(var_typeS(vec[x]));
}
puts("\nResultados: ");
printf("vec[0]= %i\n", vec[0]);
printf("vec[1]= %li\n", vec[1]);
printf("vec[2]= %s\n", vec[2]);
printf("vec[3]= %c\n", vec[3]);
printf("vec[4]= %f\n", vec[4]);
printf("vec[4]= %f\n", vec[4].data.f);
return 0;
}
Is there any way to print the value of vec[4]
without passing vec[4].data.f
to printf
?
Exit:
Int
Long Int
String
Char
Float
Resultados:
vec[0]= 37
vec[1]= 6625
vec[2]= Text
vec[3]= A
vec[4]= 0.000000
vec[4]= 13.330000
compilado em gcc-v6.3