Jump to content

Generic data types in C

- - - - -

  • Please log in to reply
6 replies to this topic

#1
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I am wondering if it's possible to use macros in C to create generic data types, where the type of the data can be inputted by the user, as in an RDBMS or similar. I was thinking I could have something like:

#define datatype(type) type

in the preprocessor section.

Then when the user inputs a data type I could do

scanf( "%s %s", type, value );

datetype(type) x = (datatype(type)) atoi(value);

or something like that (in this case the program allows the user to choose an integer type, but I could allow more options as well).

The problem with this is that type is a string, whereas the input to the datatype macro is a program token. It would be necessary to input a string into the macro function, or convert it to a generic program token, if that's even possible.

So is there any way to do this?
Programming is a journey, not a destination.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Why would you want that? What would you gain? I'm sure you can solve your problem with plain datatypes, or make structures and such. If it's possible, can you share your actual problem or is this just some thinking? :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts

Flying Dutchman said:

Why would you want that? What would you gain? I'm sure you can solve your problem with plain datatypes, or make structures and such. If it's possible, can you share your actual problem or is this just some thinking? :)

You're missing the point. I'm not trying to find a way in which I can make a single variable an arbitrary data type of my choosing. If I want that, I'll just use a union. What I want to do is make it so that the user enters a type, like say int, and then the program declares x as an int because that's what the user entered. Likewise, if the user enters char, then x will be declared as a char, and so on. I'm trying to find a way to do this that doesn't involve a long if-then statement like

if( type == "char" )

	char x = (char) value;

else if( type == "unsigned char" )

	unsigned char x = (unsigned char) value;

else if( type == "short" || type == "short int" )

	short int x = (short int) value;

else if( type == "unsigned short" || type == "unsigned short int" )

	unsigned short into x = (unsigned short int) value;

else if( type == "int" )

	int x = (int) value;

else if( type == "unsigned int" )

	unsigned int x = (unsigned int) value;

else if( type == "long" || type == "long int" )

	long int x = (long int) value;

else if( type == "unsigned long" || type == "unsigned long int" )

	unsigned long int x = (unsigned long int) value;

else if...


Of course I wouldn't have the exact same set of data types in an RDBMS. I would probably have abstract data types like VARCHAR, DATE, and INTEGER. This is just a simplified example.

All I want to know really is if there is a way to convert a string into a token that can be inputted into the macro, maybe with eval() or something like that.
Programming is a journey, not a destination.

#4
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
No eval() (well, you can look into Ch or some other C interpreter, there may be eval() thing there).
Macros are evaluated before compilation, so how do you want them to be evaluated at run-time?

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts

Xupicor said:

No eval() (well, you can look into Ch or some other C interpreter, there may be eval() thing there).
Macros are evaluated before compilation, so how do you want them to be evaluated at run-time?

Good point. It probably is impossible then.
Programming is a journey, not a destination.

#6
Xupicor

Xupicor

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
I wouldn't say it's impossible, just not the way you're trying to accomplish it. ;) There was a book that might help you out, "Object-oriented Programming With ANSI C" (Schreiner) - it uses C89 though.
Still, by the time you're going to hack through it with a working solution somebody else will just use another language and happily get over it. ;) (While you'll grow beard in front of your PC and make yet another C based language of your own :P)

So, a simpler (well, still, a lot of conditional code ;P), yet more realistic approach would be to do something you did in the post up there... With a struct and union combination maybe. A bit of macro magic could make the code to be bearable.

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
For a uninterpreted language (not like SQL) this syntax would make no sense and is in fact a security issue, a case or if statement is very valid use for this application.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users