0

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);
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • 2
    What do you mean by `way to get the actual type from a string value` ? In C strings are represented as set of characters which should be of type `char ` Like `char *` or `char a[]` .. In order calculate the length you can use `strlen()` – Gopi Jun 05 '15 at 05:02
  • @Gopi Sorry for the confusion. I actually want to know if C has anything like [Class.forName(String)](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#forName-java.lang.String-) – Jin Kwon Jun 05 '15 at 05:07
  • 1
    @JinKwon, No, there isn't. – R Sahu Jun 05 '15 at 05:11

1 Answers1

1

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?

Community
  • 1
  • 1
shreyans800755
  • 244
  • 1
  • 10