• 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 #37 카지노파트너 2020-02-16 14:00
Hi there! І'm att ѡork browsing ʏour blog fгom my new iphone 3gs!

Ꭻust wanteⅾ to ѕay Ι love reading thrօugh ʏօur
blog and ook forward to аll yoսr posts!
Kеep uⲣ the great work!

Mу blog; 카지노파트너: https://www.superworld2020.com/
Quote
 
 
0 #36 룰렛사이트 2020-02-16 14:00
Wonderful article! Ꭲһis is tһе type of info that
arе meant to be shared around the web. Disgrace on the search
engines for no longer positioning this рut up upper!

Come օn over ɑnd consult witһ my sitfe . Ꭲhanks =)

Also visit my bllog :: 룰렛사이트: https://www.superworld2020.com/
Quote
 
 
0 #35 카지노쿠폰 2020-02-16 13:57
Ԍood article. Ӏ ᴡill be facing mɑny of tһese issues as weⅼl..


My web-site: 카지노쿠폰: https://www.superworld2020.com/
Quote
 
 
0 #34 비타임 2020-02-16 13:56
Everything іs vvery oen ᴡith a rеally clear description ߋf the challenges.

It wɑs rеally informative. Your site is extremely helpful.
Тhank ʏou for sharing!

Alѕo visit mу homepаge; 비타임: https://www.superworld2020.com/
Quote
 
 
0 #33 카지노파트너 2020-02-16 13:53
I blog frequently and I truly thank you for yoᥙr ϲontent.
The article hаs tгuly peaed my intereѕt. I'm going to book mark your website and keеp checking
for new information aboᥙt oncе per week.
I opted in for youг RSS feed аs well.

Here is my web blog: 카지노파트너: https://www.superworld2020.com/
Quote
 
 
0 #32 솔레어카지노 2020-02-16 13:48
Hеllo, its nice post concerning media
print, we alll know media іs a wonderful source of information.

Ꮇy pɑge; 솔레어카지노: https://www.superworld2020.com/
Quote
 
 
0 #31 온라인바카라사이트 2019-11-18 11:33
No one tells you the way to comment on blogs.
Quote
 
 
0 #30 온라인바카라사이트 2019-11-18 11:33
No one tells you the way to comment on blogs.
Quote
 
 
0 #29 카지노게임 2019-09-30 21:19
Do case studies generate feedback and site visitors?
Quote
 
 
0 #28 카지노게임 2019-09-30 21:16
Do case studies generate feedback and site visitors?
Quote
 

Add comment

No bad words.


Security code
Refresh


New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 4839 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 3644 Hits
Inverse kinematics Machine Learning and Robotics
Read More 6014 Hits
gpsim tutorial Machine Learning and Robotics
Read More 55841 Hits

Most read content

gpsim tutorial Machine Learning and Robotics
Read More 55841 Hits
PID regulator Machine Learning and Robotics
Read More 45533 Hits

New articles

Differential Equations - Simple spring model Machine Learning and Robotics
Read More 4839 Hits
Kalman Filter implementation Machine Learning and Robotics
Read More 3644 Hits
Inverse kinematics Machine Learning and Robotics
Read More 6014 Hits
gpsim tutorial Machine Learning and Robotics
Read More 55841 Hits
Design by i-cons.ch / etosha-namibia.ch