Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How can I have an interchangeable datatype?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
canmovie




PostPosted: Fri Nov 23, 2012 10:52 am   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: Fri Nov 23, 2012 2:36 pm   Post subject: 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




PostPosted: Fri Nov 23, 2012 4:57 pm   Post subject: 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;

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




PostPosted: Sat Nov 24, 2012 1:22 am   Post subject: 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

[Edit: Oh, read the thread and not just the title. union likely won't fix that particular problem as you need to access data by field name. Excuse the interruption, go with typedef.]
SmokeMonster




PostPosted: Sat Dec 01, 2012 10:58 am   Post subject: 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;


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 ;


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




PostPosted: Sat Dec 01, 2012 8:21 pm   Post subject: Re: How can I have an interchangeable datatype?

SmokeMonster @ 2012-12-01, 10:58 am wrote:
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;


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 ;


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.


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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: