
-----------------------------------
canmovie
Fri Nov 23, 2012 10:52 am

How can I have an interchangeable datatype?
-----------------------------------
I've used ints instead of floats for most of my variables in my program, and now I have to search for every int variable, and turn it into a float.
How can I prevent this from happening again?
I've thought about declaring everything as floats in the beginning if I think I may eventually turn them into floats later on, but is there another way? 
For example, can I somehow make the datatype a variable that i can change from int to float if I want to?

What can I do while writing my program in the beginning to easily be able to change datatypes from int to float later on?

Thanks in advance.

-----------------------------------
mirhagk
Fri Nov 23, 2012 2:36 pm

RE:How can I have an interchangeable datatype?
-----------------------------------
http://www.freecprograms.com/Templates.html

I think templates (not sure of the support in C, I've only really done C++) are the best bet. You could also look into macros though.

-----------------------------------
bl0ckeduser
Fri Nov 23, 2012 4:57 pm

Re: How can I have an interchangeable datatype?
-----------------------------------
Typedefs sound like a good fit for what you wish to do.
For example, you write a typedef for a "custom" type --
[code]
typedef int mytype;
[/code]
 you can then declare variables as having that type.
To change all these variables at once you must merely change the typedef.

More info here: http://en.wikipedia.org/wiki/Typedef#Usage_examples

EDIT: BTW, C has no C++-style templates.

-----------------------------------
btiffin
Sat Nov 24, 2012 1:22 am

Re: How can I have an interchangeable datatype?
-----------------------------------
See the union keyword at http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Unions

Cheers

typedef.]

-----------------------------------
SmokeMonster
Sat Dec 01, 2012 10:58 am

Re: How can I have an interchangeable datatype?
-----------------------------------
I'm going to offer a dissenting opinion here. I would not use typedefs for stuff like this. Dealing with code that is littered with typedefs for trivial datatypes is annoying. Everytime you are looking at such code there is huge context switch for the brain to read 
[code]
my_awesome_datatype val;
[/code]

where someone has typedeffed an int to my_awesome_datatype. Worse still is when people typedef the same data type multiple times.
[code]
typedef int my_awesome_datatype ;
typedef int color ;
[/code]

You best bet is to change all ints to floats. Since c is a static language, I'm sure there are multiple tools available to assist these kinds of refactorings.

-----------------------------------
md
Sat Dec 01, 2012 8:21 pm

Re: How can I have an interchangeable datatype?
-----------------------------------
I'm going to offer a dissenting opinion here. I would not use typedefs for stuff like this. Dealing with code that is littered with typedefs for trivial datatypes is annoying. Everytime you are looking at such code there is huge context switch for the brain to read 


The solution is not to ignore typedefs, but to choose good names for your types. You might start off with a trivial typedef for your colour system, only to later change it to a structure or something like that. Maybe you were also smart and wrote a library to handle colours as well, so the only thing you need to change throughout your code is the type of variables. If you put int everywhere then you'd need to find every instance of an int and change it to your new structure if it was really a colour. If you'd used a typedef you'd only need to change one line in one header file.
