I'm learning the C programming language.
Is there any good/reasonable way to get the actual type from a string value?
I already heard the
sizeof operator is almost a compile-time thing.
Say we have the following string literal which may be provided in runtime.
char *name_of_long_long_int = "long long int";
Is there any way to do something effects following?
size_t size_of_long_long_int = sizeof(long long int);
Best How To :
No, there is not built-in function that can do such mapping.
But you can do this yourself using if-else or switch structure that is limited to your application.
There is one library called "typedef.h" //It is available in C++, I don't know about C
It has some functions like type.id() which can do reverse mapping, may be you can find a way in that lib.
//Only for C++ not for C as you requested but still you might get some idea :
You can also do this with dictionary using map
Here's very helpful link for that : Is there a way to instantiate objects from a string holding their class name?