• Increase font size
  • Default font size
  • Decrease font size

float into char array and back casting in C++

How to convert float variable (4 bytes) into an array of chars and then back? Here is the code:

 

#include 
 #include 
 #include 
 #include 
 
 using namespace std;
 
 void somefunc(unsigned int fptr);
 void somefunc2(float* fptr);
 
 int main()
 {
 float a = 1.345678;
 char cArray[1024];
 sprintf(cArray, "%f", a);
 
 cout << "Float converted into char array: " << cArray << endl;
 
 float b = atof(cArray);
 cout << "Char array converted into float: " << b << endl;
 
 //-------------------
 // POINTERS
 // http://www.cplusplus.com/doc/tutorial/pointers/
 //-------------------
 float fl=3.14;
 unsigned int addr=(unsigned int) &fl;
 somefunc(addr);
 cout << "Variable accessed by pointer: " << fl << endl;
 
 //-------------------
 float f2=6.14;
 float* addr2 = &f2;
 somefunc2(addr2);
 cout  << "Variable accessed by pointer: " << f2 << endl;
 
 cout << sizeof(char *) << ", " << sizeof(float *) << endl;
 
 int *abc = NULL;
 cout << "Prints sizeof(int): " << abc + 1 << endl; /* Prints sizeof(int) */
 
 //Prints 1. Casting it back to void * is necessary, otherwise it will try to dereference it and print as a string.
 cout << (void *)((char *)abc + 1) << endl;
 
 return 0;
 }
 
 void somefunc(unsigned int fptr){
 *(float*)fptr=99.9;
 }
 
 void somefunc2(float* fptr){
 *fptr=88.8;
 }
 

 

 

Comments  

 
0 #27 카지노 2019-09-19 04:22
This is such a EFFECTIVELY EXPLAINED article.
Quote
 
 
0 #26 카지노 2019-09-19 04:21
This is such a EFFECTIVELY EXPLAINED article.
Quote
 
 
0 #25 카지노 2019-09-19 04:21
This is such a EFFECTIVELY EXPLAINED article.
Quote
 
 
0 #24 카지노 2019-09-19 04:20
This is such a EFFECTIVELY EXPLAINED article.
Quote
 
 
0 #23 bigmarket 2019-08-22 08:14
Отличный интернет магазин хорошие скидки и бесплатная доставка
http://bigmarket24.ru
Quote
 
 
0 #22 BestDavida 2019-08-15 04:37
I see you don't monetize concordia.sk, don't waste
your traffic, you can earn extra bucks every month with new monetization method.
This is the best adsense alternative for any type of website (they approve
all websites), for more info simply search in gooogle: murgrabia's tools
Quote
 
 
0 #21 바카라사이트 2019-07-17 04:40
Thank you for another fantastic post. Where else may anybody get that kind of information in such a perfect
Quote
 
 
0 #20 바카라사이트 2019-07-17 04:38
It’s an amazing paragraph in favor of all the online visitors; they will take benefit from it I am sure.
Quote
 
 
0 #19 바카라사이트 2019-07-17 04:36
Wow, this piece of writing is fastidious, my sister is analyzing these things, so I am going to inform her.
Quote
 
 
0 #18 카지노사이트 2019-07-17 04:33
It’s really a great and helpful piece of information. I am glad that you shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 4644 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 3394 Hits
Inverse kinematics Machine Learning and Robotics
Read More 4979 Hits
gpsim tutorial Machine Learning and Robotics
Read More 48021 Hits

Most read content

gpsim tutorial Machine Learning and Robotics
Read More 48021 Hits
PID regulator Machine Learning and Robotics
Read More 40484 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 4644 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 3394 Hits
Inverse kinematics Machine Learning and Robotics
Read More 4979 Hits
gpsim tutorial Machine Learning and Robotics
Read More 48021 Hits
Design by i-cons.ch / etosha-namibia.ch