B
Baloff
Hello
I am struggling with this exercise
**********************************************************************
Define a float variable. Take its address, cast that address to an
unsigned char, and assign it to an unsigned char pointer. Using this
pointer and [ ], index into the float variable and use the
printBinary( ) function defined in this chapter to print out a map
of the float (go from 0 to sizeof(float)). Change the value of the
float and see if you can figure out what\u2019s going on (the float
contains encoded data).
the printBinary.cpp is
#include "printBinary.h"
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val & (1 << i))
cout << "1";
else
cout << "0";
}
**********************************************************************
my initial code which puts out the error below is as follows
******************************code******************************
#include <iostream>
#include "../printBinary.h"
using namespace std;
int main(){
float f = 3.3;
unsigned char* ucp = (unsigned char) &f;
for (int i = 0; i < sizeof(f); ++i){
ucp = i;
printBinary(f);
}
}
******************************error******************************
main.cpp: In function `int main()':
main.cpp:8: warning: cast from pointer to integer of different size
main.cpp:8: error: invalid conversion from `unsigned char' to `unsigned char*'
main.cpp:12: error: invalid types `float[int]' for array subscript
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.
I am struggling with this exercise
**********************************************************************
Define a float variable. Take its address, cast that address to an
unsigned char, and assign it to an unsigned char pointer. Using this
pointer and [ ], index into the float variable and use the
printBinary( ) function defined in this chapter to print out a map
of the float (go from 0 to sizeof(float)). Change the value of the
float and see if you can figure out what\u2019s going on (the float
contains encoded data).
the printBinary.cpp is
#include "printBinary.h"
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
for(int i = 7; i >= 0; i--)
if(val & (1 << i))
cout << "1";
else
cout << "0";
}
**********************************************************************
my initial code which puts out the error below is as follows
******************************code******************************
#include <iostream>
#include "../printBinary.h"
using namespace std;
int main(){
float f = 3.3;
unsigned char* ucp = (unsigned char) &f;
for (int i = 0; i < sizeof(f); ++i){
ucp = i;
printBinary(f);
}
}
******************************error******************************
main.cpp: In function `int main()':
main.cpp:8: warning: cast from pointer to integer of different size
main.cpp:8: error: invalid conversion from `unsigned char' to `unsigned char*'
main.cpp:12: error: invalid types `float[int]' for array subscript
make: *** [main.o] Error 1
make: Target `proj1' not remade because of errors.