Since the pointer returned by malloc()
is sufficiently well aligned to be used as a pointer to any type, you could (but probably shouldn’t) use:
assert(sizeof(float) <= 10);
void *data = malloc(10);
char *str = data;
strcpy(str, "123.4");
float *vp = data;
*vp = strtof(str, NULL);
…use *vp…
free(data);
And now you can use *vp
for the float
value. However, that’s ridiculous compared with:
char *str = malloc(10);
strcpy(str, "123.4");
float value = strtof(str, NULL);
…use value…
free(str);
which is basically your original code — that you decided for unclear reasons was not what you wanted to do.
1
solved Can I change the datatype of previously declared variable in C?